1

I'm writing a script to automate adding, committing, and pushing changes to my submodule and repo. Some submodules are nested further than others and I need to cd back up to the root of the repo. I'm not using the git submodule foreach pattern because there may be changes in other submodules that I don't want to commit to them (a quick test) from the branch I'm working on. I have a branch in the super-repo set up for each submodule, conveniently named such that using cd with it will move down to the repo.

I found this question which has answers that relate to creating and/or aliasing a function to cd up n-levels. I could do something like this as I've already got some aliased functions for other actions, but it would require a different function for every directory or a different script, thus different hotkey, in Git Extensions that's at a different level.

I'm using Git Extensions to manage my repo and it provides a scripting interface in its settings. My current function under the alias in the .git/config:

applyupdate = "!f() { \
                      cd \"$1\"/;\
                      git add .;\
                      git commit -m \"$2\";\
                      git push;\
                      cd ..;\ #####this needs to be n levels
                      git add \"$1\";\
                      git commit -m \"$2\";\
                      git push;\
                    }; f"

This is executed in Git Extension via a hotkey which runs it with the following arguments:

applyupdate {cBrnach} {{UserInput}}

where {cBranch} will be the active branch or ask for a branch if multiple lie on the same commit in the graph
and {{UserInput}} will bring up a dialog to insert a quoted commit message.

Can cd n-levels be done with a single function and in a way that I don't need to input how many levels I need to go up? I don't think the {UserInput} option in Git Extensions works with multiple inputs.

  • Do you always want to go the top of the repository or will n vary? – Arkadiusz Drabczyk Sep 24 '19 at 17:13
  • @ArkadiuszDrabczyk for that cd statement, it should return to the top of the repo (which could be n levels up). This is the only instance where I'm trying to do something like this and I don't think I would need to do this with any other folders. – zecuse Sep 24 '19 at 17:18
  • 1
    You can use this to go the top of the repo: cd "$(git rev-parse --show-toplevel)" – Arkadiusz Drabczyk Sep 24 '19 at 17:26
  • @ArkadiuszDrabczyk Thanks! Didn't know about that command! – zecuse Sep 24 '19 at 17:34
  • Does this answer your question? – Arkadiusz Drabczyk Sep 24 '19 at 17:35
0

Thanks to Arkadiusz Drabczyk for mentioning git rev-parse --show-toplevel.
Changing the script to this works for me:

applyupdate = "!f() { \
                      cd \"$1\"/;\
                      git add .;\
                      git commit -m \"$2\";\
                      git push;\
                      cd ..;\ #this gets me out of the submodule
                      cd \"$(git rev-parse --show-toplevel)\";\ #this return to super-repo's root
                      git add \"$1\";\
                      git commit -m \"$2\";\
                      git push;\
                    }; f"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.