Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Git submodule foreach?

I have sup aliased to submodule foreach 'git co master; git up' (co & up are aliases for checkout & pull --rebase, respectively.).

How do add a condition so that if the submodule name is Libraries/JSONKit, it checks out the branch named experimental, instead of master?

like image 849
ma11hew28 Avatar asked Dec 03 '11 01:12

ma11hew28


1 Answers

The script passwd to git submodule is run with it's working directory set to the top of the given submodule...so you can simply look at pwd to see if you're in you're in the particular submodule. However, if you take some time to read the git submodule documentation, it turns out to be even easier:

foreach
    Evaluates an arbitrary shell command in each checked out submodule. The 
    command has access to the variables $name, $path, $sha1
    and $toplevel: $name is the name of the relevant submodule section in 
    .gitmodules, $path is the name of the submodule directory
    relative to the superproject, $sha1 is the commit as recorded in the superproject, 
    and $toplevel is the absolute path to the top-level of the superproject.

So you can do something like this:

git submodule foreach '[ "$path" = "Libraries/JSONKit" ] \
  && branch=experimental \
  || branch=master; git co $branch'
like image 154
larsks Avatar answered Oct 03 '22 01:10

larsks