3

I need to automate the following step:

git clone A && cd A && (git clone B; git clone C)

I cannot use submodules. I cannot use a git wrapper (already existent or by writing some function in my *$shell_rc).

3
  • Since you are cloning B and C inside A, have you tried using git submodules? It won't necessarily automate things, but can be a better way of organizing your repos. Commented Dec 21, 2015 at 9:02
  • is there a way to say to git submodules to always fetch master and not a specific commit? Commented Dec 21, 2015 at 9:05
  • Yup, see VonC's answer ;) Commented Dec 21, 2015 at 9:09

3 Answers 3

2

I would simply add a script addExternals to the top-dir of the repo, and mention it in the README. You might consider adding a call of it to the build system, but I think it's best if you leave the control of the subrepositories to the user.

The reason why I believe that this is the best approach: You cannot influence your users before they have cloned the repository, and they need to be aware of the fact that you are relying on subrepositories which are not modules. If you just invoke some magic to get the subrepositories into place, your users will trip over it later when one of the subrepositories needs to be updated to make the main repo compile again.

Sign up to request clarification or add additional context in comments.

4 Comments

"If you just invoke some magic to get the subrepositories into place, your users will trip over it later when one of the subrepositories needs to be updated to make the main repo compile again." - so true!
@ilpianista While I agree with exposing subrepositories to the users, having a script to be manually called sounds like reinventing the submodules wheel. Why exactly you cannot use submodules?
@AntonioPérez the problem with them is that you manually have to update project A to point to the latest commit of B and C everytime you commit on them. I like submodules, but my boss doesn't like this behavior I just described.
But you would have to explicitly call the addExternals script anyway when you commit on B or C in order to update A. So I don't see any benefit, and I see the disadvantage of maintaining the script yourself.
2

You can use an alias

alias gitclones='git clone A && cd A && (git clone B; git clone C)'

Or you can define a custom wrapper (not git itself but a bash script in the user's path)

But the git way would be to use submodules, in which case a simple git clone --recursive --remote would be enough.
You can make a submodule to follow a branch like master.
See "Git submodules: Specify a branch/tag"

Note that repo B and C will be consider as nested repo, only their gitlink will be recorded in A.

12 Comments

@ilpianista git aliases are not shell configurations, they're stored in ~/.gitconfig
@ilpianista I have edited my answer: you can make a submodule follow a branch
@KeillЯandor true, but I cannot touch .gitconfig neither. The user that fetches the code doesn't have to know that project A needs B and C.
@VonC didn't know that! I'm reading stackoverflow.com/questions/9189575/… right now
@VonC the problem with submodules is that gitlink. project A always should point to latest master of B and C.
|
1

You could use a post-checkout hook. Thus, you could have your repositories B and C getting updated if user switches branches/refs in A.

5 Comments

Can I ship an hook with a repo? Or it has to be downloaded first?
No, it would require each clone to have the hooks manually initialized (you may have a script to provide a single-step initialization). See stackoverflow.com/q/427207/198011
Then basically this is the same of shipping a script with project A and tell people to run that script to fetch subprojects.
Well, they will need to run that script only once, then the hook would take care of keeping B & C updated if it is required. If B & C are not expected to change across branches, the hook is probably not a good solution to your needs.
You are correct when you say that branches/refs could change, so yes the hook is a better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.