@@ -416,15 +416,15 @@ static int add_worktree(const char *path, const char *refname,
416416 struct strbuf sb_git = STRBUF_INIT , sb_repo = STRBUF_INIT ;
417417 struct strbuf sb = STRBUF_INIT , realpath = STRBUF_INIT ;
418418 const char * name ;
419- struct child_process cp = CHILD_PROCESS_INIT ;
420419 struct strvec child_env = STRVEC_INIT ;
421420 unsigned int counter = 0 ;
422421 int len , ret ;
423422 struct strbuf symref = STRBUF_INIT ;
424423 struct commit * commit = NULL ;
425424 int is_branch = 0 ;
426425 struct strbuf sb_name = STRBUF_INIT ;
427- struct worktree * * worktrees ;
426+ struct worktree * * worktrees , * wt = NULL ;
427+ struct ref_store * wt_refs ;
428428
429429 worktrees = get_worktrees ();
430430 check_candidate_path (path , opts -> force , worktrees , "add" );
@@ -495,20 +495,32 @@ static int add_worktree(const char *path, const char *refname,
495495 strbuf_realpath (& realpath , get_git_common_dir (), 1 );
496496 write_file (sb_git .buf , "gitdir: %s/worktrees/%s" ,
497497 realpath .buf , name );
498- /*
499- * This is to keep resolve_ref() happy. We need a valid HEAD
500- * or is_git_directory() will reject the directory. Any value which
501- * looks like an object ID will do since it will be immediately
502- * replaced by the symbolic-ref or update-ref invocation in the new
503- * worktree.
504- */
505- strbuf_reset (& sb );
506- strbuf_addf (& sb , "%s/HEAD" , sb_repo .buf );
507- write_file (sb .buf , "%s" , oid_to_hex (null_oid ()));
508498 strbuf_reset (& sb );
509499 strbuf_addf (& sb , "%s/commondir" , sb_repo .buf );
510500 write_file (sb .buf , "../.." );
511501
502+ /*
503+ * Set up the ref store of the worktree and create the HEAD reference.
504+ */
505+ wt = get_linked_worktree (name , 1 );
506+ if (!wt ) {
507+ ret = error (_ ("could not find created worktree '%s'" ), name );
508+ goto done ;
509+ }
510+ wt_refs = get_worktree_ref_store (wt );
511+
512+ ret = refs_init_db (wt_refs , REFS_INIT_DB_IS_WORKTREE , & sb );
513+ if (ret )
514+ goto done ;
515+
516+ if (!is_branch && commit )
517+ ret = refs_update_ref (wt_refs , NULL , "HEAD" , & commit -> object .oid ,
518+ NULL , 0 , UPDATE_REFS_MSG_ON_ERR );
519+ else
520+ ret = refs_create_symref (wt_refs , "HEAD" , symref .buf , NULL );
521+ if (ret )
522+ goto done ;
523+
512524 /*
513525 * If the current worktree has sparse-checkout enabled, then copy
514526 * the sparse-checkout patterns from the current worktree.
@@ -526,22 +538,6 @@ static int add_worktree(const char *path, const char *refname,
526538
527539 strvec_pushf (& child_env , "%s=%s" , GIT_DIR_ENVIRONMENT , sb_git .buf );
528540 strvec_pushf (& child_env , "%s=%s" , GIT_WORK_TREE_ENVIRONMENT , path );
529- cp .git_cmd = 1 ;
530-
531- if (!is_branch && commit ) {
532- strvec_pushl (& cp .args , "update-ref" , "HEAD" ,
533- oid_to_hex (& commit -> object .oid ), NULL );
534- } else {
535- strvec_pushl (& cp .args , "symbolic-ref" , "HEAD" ,
536- symref .buf , NULL );
537- if (opts -> quiet )
538- strvec_push (& cp .args , "--quiet" );
539- }
540-
541- strvec_pushv (& cp .env , child_env .v );
542- ret = run_command (& cp );
543- if (ret )
544- goto done ;
545541
546542 if (opts -> orphan &&
547543 (ret = make_worktree_orphan (refname , opts , & child_env )))
@@ -587,6 +583,7 @@ static int add_worktree(const char *path, const char *refname,
587583 strbuf_release (& sb_git );
588584 strbuf_release (& sb_name );
589585 strbuf_release (& realpath );
586+ free_worktree (wt );
590587 return ret ;
591588}
592589
0 commit comments