I need to extend a given tool with a Bash script that is supposed to work with Linux and MacOS. The script gets 2 parameters:
- A repository location (file system, ssh, http(s), ...)
- A commitish, e.g. branch, tag, commit hash
I have no influence on the parameters
The result of the script's run should be that
- the repository is cloned to a fixed destination (always the same for one repository)
- the repositorie's working tree should correspond to the latest state of the comittish (e.g. if it was a branch the tip of that branch)
If the repository does not (yet) exist locally, the procedure is as simple as
git clone $REPO_SOURCE $REPO_DIR
cd $REPO_DIR
git checkout $REPO_REF
My question: Consider a repository is already cloned to /repos/foo
. After an obvios git fetch
, how to I update that repository to the provided $REPO_REF
?
- If
$REPO_REF
was a branch, agit checkout $REPO_REF && git pull
should work - If it was a commit hash, there was no update needed (just
git checkout $REPO_REF
?) - If it was a tag, then the tag might have been moved on the origin, how to handle this?
- How to handle other edge cases?
Is there a simple reset-repository-to-this-commitsh
way, so the repository behaves just as if it was freshly cloned?
Side nodes:
- The same repository might be used with different commitish's, but only sequentially: It is guaranteed that the script isn't invoked more than once at the same time
- All external changes to the repository might be always discarded without notification
- While deleting and cloning the repository would work, it is impractical due to their sizes and it being an ugly solution
- No (git) changes are needed, so checking out a detached head is okay