sourcegit-scm/sourcegit

[Feature Request] Create Merge/Pull Request from branch context menu

p-fischer opened this issue · 3 comments

It would be a great workflow improvement if merge/pull requests could be initiated by right-clicking on a branch, clicking on the context menu option "Create Merge Request on origin", which will re-direct to the brower.

While the integration with e.g. GitHub and GitLab would be ideal, I saw in #250 concern was expressed about these integrations.

As a fallback I suggest to add support for custom actions on branch level, so users can replicate the "Create Merge Request" context menu option themselves.

I successfully tested this on Ubuntu 24.10.
Awesome! Thank you very much!

I successfully tested this on Ubuntu 24.10. Awesome! Thank you very much!

@p-fischer Can you share an example of how to setup the custom action to create a pull request in github (for example)?

I setup a custom action so I can right-click on a branch name in the left column, select "Custom Action" and click on my custom action "Create Merge Request", which will open (in my case) GitLab in a website with the correct branches selected.
Building this for GitHub should work in the equivalent way.

Steps to create the custom action:

  1. Open repository in SourceGit
  2. Open repository settings (That's the settings icon left of the fetch icon. Don't confuse the repository settings with the global settings. A dialog with title "Repository Configure" will open.)
  3. Click on tab "CUSTOM ACTION"
  4. Click on + icon on the bottom left
  5. Pick name for the custom action (e.g. "Create Pull Request" or "Create Merge Request")
  6. Select scope "Branch" (At least if you want the website to be opened with preselected branches)
  7. Skip input controls
  8. (Outside of SourceGit) Create a script (see below about the script content)
  9. Make script executable (using chmod +x FILENAME on Linux)
  10. (Back to SourceGit dialog) Link script in "Executable File"
  11. For Arguments set ${BRANCH}
  12. Check wait for action exit

regarding 8:

Create a script file. Under Linux I chose the path ~/.config/SourceGit/scripts/push_and_create_merge_request_from_branch.sh

My script looks like this:

#!/bin/bash

current_branch=$1 # the branch you clicked on
target_branch=main # the branch you want to merge into
remote=origin
gitlab_repo=https://[Enter project URL here]

# push the current_branch
git push ${remote} ${current_branch}:${current_branch}

# open GitLab website to create merge request
xdg-open ${gitlab_repo}/merge_requests/new?merge_request[source_branch]=${current_branch}&merge_request[target_branch]=${target_branch}

Some notes on the script

  • Pushing the current branch is optional. I personally added it for convenience.
  • xdg-open is a Linux specific command to open a URL using the default browser. If you're on Windows or MacOS you need to change it.
  • The script, especially the last line is specific to GitLab and how the URL for creating a merge request looks like. For GitHub start creating a new pull request in the web, copy the URL and replace the concrete branch names with the variables source_branch and current_branch. You could extract the repository URL into a variable like I did but you don't need to. This might be nice if you want to re-use the script.
  • Your target/default branch might have a different name than main. If so change it.

... OR as an alternative for GitHub for the script you could install GitHub CLI and use the PR create command