@@ -352,26 +352,24 @@ static void append_suffix(int depth, const struct object_id *oid, struct strbuf
352352 repo_find_unique_abbrev (the_repository , oid , abbrev ));
353353}
354354
355- static void describe_commit (struct object_id * oid , struct strbuf * dst )
355+ static void describe_commit (struct commit * cmit , struct strbuf * dst )
356356{
357- struct commit * cmit , * gave_up_on = NULL ;
357+ struct commit * gave_up_on = NULL ;
358358 struct lazy_queue queue = LAZY_QUEUE_INIT ;
359359 struct commit_name * n ;
360360 struct possible_tag all_matches [MAX_TAGS ];
361361 unsigned int match_cnt = 0 , annotated_cnt = 0 , cur_match ;
362362 unsigned long seen_commits = 0 ;
363363 unsigned int unannotated_cnt = 0 ;
364364
365- cmit = lookup_commit_reference (the_repository , oid );
366-
367365 n = find_commit_name (& cmit -> object .oid );
368366 if (n && (tags || all || n -> prio == 2 )) {
369367 /*
370368 * Exact match to an existing ref.
371369 */
372370 append_name (n , dst );
373371 if (n -> misnamed || longformat )
374- append_suffix (0 , n -> tag ? get_tagged_oid (n -> tag ) : oid , dst );
372+ append_suffix (0 , n -> tag ? get_tagged_oid (n -> tag ) : & cmit -> object . oid , dst );
375373 if (suffix )
376374 strbuf_addstr (dst , suffix );
377375 return ;
@@ -528,39 +526,47 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
528526}
529527
530528struct process_commit_data {
531- struct object_id current_commit ;
532- struct object_id looking_for ;
529+ struct commit * current_commit ;
530+ const struct object_id * looking_for ;
533531 struct strbuf * dst ;
534532 struct rev_info * revs ;
535533};
536534
537535static void process_commit (struct commit * commit , void * data )
538536{
539537 struct process_commit_data * pcd = data ;
540- pcd -> current_commit = commit -> object . oid ;
538+ pcd -> current_commit = commit ;
541539}
542540
543541static void process_object (struct object * obj , const char * path , void * data )
544542{
545543 struct process_commit_data * pcd = data ;
546544
547- if (oideq (& pcd -> looking_for , & obj -> oid ) && !pcd -> dst -> len ) {
545+ if (oideq (pcd -> looking_for , & obj -> oid ) && !pcd -> dst -> len ) {
548546 reset_revision_walk ();
549- describe_commit (& pcd -> current_commit , pcd -> dst );
550- strbuf_addf (pcd -> dst , ":%s" , path );
547+ if (pcd -> current_commit ) {
548+ describe_commit (pcd -> current_commit , pcd -> dst );
549+ strbuf_addf (pcd -> dst , ":%s" , path );
550+ }
551551 free_commit_list (pcd -> revs -> commits );
552552 pcd -> revs -> commits = NULL ;
553553 }
554554}
555555
556- static void describe_blob (struct object_id oid , struct strbuf * dst )
556+ static void describe_blob (const struct object_id * oid , struct strbuf * dst )
557557{
558558 struct rev_info revs ;
559559 struct strvec args = STRVEC_INIT ;
560- struct process_commit_data pcd = { * null_oid (the_hash_algo ), oid , dst , & revs };
560+ struct object_id head_oid ;
561+ struct process_commit_data pcd = { NULL , oid , dst , & revs };
562+
563+ if (repo_get_oid (the_repository , "HEAD" , & head_oid ))
564+ die (_ ("cannot search for blob '%s' on an unborn branch" ),
565+ oid_to_hex (oid ));
561566
562567 strvec_pushl (& args , "internal: The first arg is not parsed" ,
563- "--objects" , "--in-commit-order" , "--reverse" , "HEAD" ,
568+ "--objects" , "--in-commit-order" , "--reverse" ,
569+ oid_to_hex (& head_oid ),
564570 NULL );
565571
566572 repo_init_revisions (the_repository , & revs , NULL );
@@ -574,6 +580,9 @@ static void describe_blob(struct object_id oid, struct strbuf *dst)
574580 reset_revision_walk ();
575581 release_revisions (& revs );
576582 strvec_clear (& args );
583+
584+ if (!dst -> len )
585+ die (_ ("blob '%s' not reachable from HEAD" ), oid_to_hex (oid ));
577586}
578587
579588static void describe (const char * arg , int last_one )
@@ -590,10 +599,10 @@ static void describe(const char *arg, int last_one)
590599 cmit = lookup_commit_reference_gently (the_repository , & oid , 1 );
591600
592601 if (cmit )
593- describe_commit (& oid , & sb );
602+ describe_commit (cmit , & sb );
594603 else if (odb_read_object_info (the_repository -> objects ,
595604 & oid , NULL ) == OBJ_BLOB )
596- describe_blob (oid , & sb );
605+ describe_blob (& oid , & sb );
597606 else
598607 die (_ ("%s is neither a commit nor blob" ), arg );
599608
0 commit comments