lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

Automatically push root git repository on submodule push

Open haras-unicorn opened this issue 2 years ago • 11 comments

Is your feature request related to a problem? Please describe. I have a repository that has a submodule and a lot of the time I'm just commiting and pushing the submodule which is okay but then I have to commit and push the changes to the root repository manually. It would be nice if I could somehow do this without manual intervention. I don't know if this is already supported or if lazygit is the right way to go about this but it would be nice if I could make it work this way.

Describe the solution you'd like After I push the submodule repository I'd like to be prompted for a commit message for the root repository and then the root repository gets pushed as well. The commit for the root repository would only include the changes in the submodule repository.

Describe alternatives you've considered I guess I could just position myself in the root repository and it would automatically push the submodule repository whenever I try to push the root, but that's also a hassle because I have to switch working directories. A shell function would help but then I'd have to commit in the submodule repository, exit lazygit, do the shell function which positions me to the root repository, opens lazygit so I can commit and push there, and returns me back to the submodule repository.

Additional context I guess that's it.

haras-unicorn avatar Aug 07 '22 11:08 haras-unicorn

but that's also a hassle because I have to switch working directories.

Well you can enter the submodule by pressing Enter on it and exit by pressing Escape, does that help?

A shell function would help but then I'd have to commit in the submodule repository, exit lazygit, do the shell function which positions me to the root repository, opens lazygit so I can commit and push there, and returns me back to the submodule repository

Have you tried running it from within lazygit by pressing :?

Longer explanation: You can reach the Submodules tab by pressing ] or [ in the Files pane, it's reachable by pressing number 2. There you can enter a submodule as if you've started lazygit there. Pressing Escape returns you to the root repo.

As for running a shell command, by pressing : you get a menu to run a custom command which will run in the background, and it will be added to the suggestions area which acts as a command history.

mark2185 avatar Aug 07 '22 12:08 mark2185

This could also be a good candidate for a pre-set custom command. See https://github.com/jesseduffield/lazygit/wiki/Custom-Commands-Compendium

It's much easier to implement features if a user has created a custom command for it first, because it's clearer what the expected behaviour is

jesseduffield avatar Aug 08 '22 08:08 jesseduffield

@mark2185 those features sound cool, but it like something really automatic like @jesseduffield is proposing i think

ill keep you updated here whether i manage to do what i need to with custom commands

PS forgive my ignorance - im still new to lazygit

haras-unicorn avatar Aug 08 '22 16:08 haras-unicorn

@Hrle97 we're talking about the same thing 😅

When you do manage to create a custom command, we'll add it to the compendium for other users to use.

mark2185 avatar Aug 08 '22 16:08 mark2185

Seems like it is working:

customCommands:
  - key: '<c-c>'
    description: 'commit with supermodules'
    prompts:
      - type: 'input'
        title: 'Message'
    command: 'git commit -m "{{index .PromptResponses 0}}" && wd="$(pwd)" && cd .. && cd "$(git rev-parse --show-toplevel)" && git add "$wd" && git commit -m "update module $wd" && cd "$wd"'
    subprocess: true
    context: 'global'

  - key: '<c-p>'
    description: 'push with supermodules'
    command: 'git push && wd="$(pwd)" && cd .. && cd "$(git rev-parse --show-toplevel)" && git push && cd "$wd"'
    subprocess: true
    context: 'global'

haras-unicorn avatar Aug 08 '22 18:08 haras-unicorn

@jesseduffield should we maybe make command to be an array? So we can make a list of commands to be executed?

mark2185 avatar Aug 08 '22 18:08 mark2185

Is this better? Fixed a possible issue as well.

customCommands:
  - key: '<c-c>'
    description: 'commit with supermodules'
    prompts:
      - type: 'input'
        title: 'Message'
    command: >-
      git commit -m "{{index .PromptResponses 0}}" &&
      wd=$(pwd) &&
      mod="$(git rev-parse --show-toplevel)" &&
      cd "$mod" &&
      cd .. &&
      sup="$(git rev-parse --show-toplevel)" &&
      cd "$sup" &&
      mod="$(realpath --relative-to="$sup" "$mod")" &&
      git add "$mod" &&
      git commit -m "update module $mod" &&
      cd "$wd"
    subprocess: true
    context: 'global'

  - key: '<c-p>'
    description: 'push with supermodules'
    command: >-
      git push &&
      wd=$(pwd) &&
      mod="$(git rev-parse --show-toplevel)" &&
      cd "$mod" &&
      cd .. &&
      sup="$(git rev-parse --show-toplevel)" &&
      cd "$sup" &&
      git push &&
      cd "$wd"
    subprocess: true
    context: 'global'

haras-unicorn avatar Aug 08 '22 19:08 haras-unicorn

Is this better? Fixed a possible issue as well.

Definitely more readable.

You can get the superproject path (I'm guessing that's what $sup is) by running --show-superproject-working-tree from the submodule.

Also note that your command requires the user to have realpath. Do you really need to be at the root of the superproject?

mark2185 avatar Aug 08 '22 19:08 mark2185

realpath is just for getting the relative path of the module (maybe there's a way through git?). I don't need to be in the root, but it's a good starting commit message. I'll edit it some more later.

haras-unicorn avatar Aug 08 '22 19:08 haras-unicorn

realpath is just for getting the relative path of the module

I know, but not every system has realpath.

I don't need to be in the root, but it's a good starting commit message.

Well I think "update module $wd" is just as good, you most likely have only one submodule named test-data or 3rd-party/libusb or something, you'll see in the git diff where exactly is it located, but everybody will probably know which one the update is referring to when you see the commit message.

mark2185 avatar Aug 08 '22 19:08 mark2185

@jesseduffield should we maybe make command to be an array? So we can make a list of commands to be executed?

I reckon it's up to the user to handle long commands themselves, as we've seen here. Another option is to have a bash script somewhere that you call passing in lazygit-based arguments.

jesseduffield avatar Aug 08 '22 21:08 jesseduffield

ill close this for now since im fine with this solution and if anyone else wants to use it its here

haras-unicorn avatar Oct 24 '22 19:10 haras-unicorn