directory: Fix a couple of edge case issues in checkout_repository_ref
Ensure to remove trailing slash in git URL when computing its basename as an empty string is returned otherwise.
When a shallow fetch failed, typically when the ref is a commit short hash, retry a full fetch in order for ref checkout to succeed.
Related to #4751.
Fixes SWH-LOADER-GIT-1YA and SWH-LOADER-GIT-1YB.