@@ -230,20 +230,21 @@ static int add_worktree(const char *path, const char *refname,
230230 int counter = 0 , len , ret ;
231231 struct strbuf symref = STRBUF_INIT ;
232232 struct commit * commit = NULL ;
233+ int is_branch = 0 ;
233234
234235 if (file_exists (path ) && !is_empty_dir (path ))
235236 die (_ ("'%s' already exists" ), path );
236237
237238 /* is 'refname' a branch or commit? */
238239 if (!opts -> detach && !strbuf_check_branch_ref (& symref , refname ) &&
239- ref_exists (symref .buf )) { /* it's a branch */
240+ ref_exists (symref .buf )) {
241+ is_branch = 1 ;
240242 if (!opts -> force )
241243 die_if_checked_out (symref .buf , 0 );
242- } else { /* must be a commit */
243- commit = lookup_commit_reference_by_name (refname );
244- if (!commit )
245- die (_ ("invalid reference: %s" ), refname );
246244 }
245+ commit = lookup_commit_reference_by_name (refname );
246+ if (!commit )
247+ die (_ ("invalid reference: %s" ), refname );
247248
248249 name = worktree_basename (path , & len );
249250 git_path_buf (& sb_repo , "worktrees/%.*s" , (int )(path + len - name ), name );
@@ -308,7 +309,7 @@ static int add_worktree(const char *path, const char *refname,
308309 argv_array_pushf (& child_env , "%s=%s" , GIT_WORK_TREE_ENVIRONMENT , path );
309310 cp .git_cmd = 1 ;
310311
311- if (commit )
312+ if (! is_branch )
312313 argv_array_pushl (& cp .args , "update-ref" , "HEAD" ,
313314 oid_to_hex (& commit -> object .oid ), NULL );
314315 else
@@ -339,6 +340,15 @@ static int add_worktree(const char *path, const char *refname,
339340 strbuf_addf (& sb , "%s/locked" , sb_repo .buf );
340341 unlink_or_warn (sb .buf );
341342 }
343+
344+ /*
345+ * Hook failure does not warrant worktree deletion, so run hook after
346+ * is_junk is cleared, but do return appropriate code when hook fails.
347+ */
348+ if (!ret && opts -> checkout )
349+ ret = run_hook_le (NULL , "post-checkout" , oid_to_hex (& null_oid ),
350+ oid_to_hex (& commit -> object .oid ), "1" , NULL );
351+
342352 argv_array_clear (& child_env );
343353 strbuf_release (& sb );
344354 strbuf_release (& symref );
0 commit comments