Skip to content

Instantly share code, notes, and snippets.

@Azeirah
Last active September 5, 2024 21:16
Show Gist options
  • Save Azeirah/542f1db12e3ef904abfc7e9c2e83310e to your computer and use it in GitHub Desktop.
Save Azeirah/542f1db12e3ef904abfc7e9c2e83310e to your computer and use it in GitHub Desktop.
Setting up magit forge for gitlab in Doom Emacs

Magit forge for Gitlab in Doom Emacs

This guide assumes you're working on a Unix-like environment. I'm using Linux Mint (Ubuntu). It should work on Macs, and might on Windows.

These are the four steps you can use as a checklist, see the headings below for the details on each step.

  • Enable forge support
  • Create a Gitlab API key
  • Add your gitlab credentials to ~/.authinfo.gpg
  • Set-up forge in emacs

Enable forge support

First, enable forge support in doom. Go to ~/.doom.d/init.el, and replace the magit module with (magit +forge).

For clarity, it should look somewhat like this

       lsp
       ;;macos             ; MacOS-specific commands
       (magit +forge)      ; a git porcelain for Emacs
       ;;make              ; run make tasks from Emacs
       ;;pass              ; password manager for nerds

You'll have to run doom sync and restart emacs. In emacs, you can use M-x doom/reload or <SPC h r r>

Create a Gitlab API key

Create a Gitlab API key for the account you'll be using to access your repositories, write this one down. I'm not entirely sure what rights you should give the repo, but I gave it all read rights, and write rights to the repository itself.

Add your gitlab credentials to ~/.authinfo.gpg

Edit or create the file ~/.authinfo.gpg. If it's new, paste the following. Fill in your email address, your gitlab username and your api key where the {{LOUD-BRACKETS}} are.

# -*- epa-file-encrypt-to: ("{{YOUR-EMAIL-ADDRESS}}") -*-
machine gitlab.com/api/v4 login {{YOUR-GITLAB-USERNAME}}^forge password {{YOUR-API-KEY}}

NOTE THE NEWLINE AT THE END, the authinfo format is very sensitive.

Set-up forge in emacs

In emacs, open a magit buffer for a or the repo you want to use forge with. Run the command M-x forge-add-repository. Note, if you have a remote named origin (as is the convention) in this repository, forge will automatically fill in the right info. If you don't, you'll have to type in the remote yourself. IE git@gitlab.com:{{your-user}}/{{your-repo}}

Run forge commands right from Magit

In a Magit buffer, press @ (discover via the popup of popups with ?). This will list all available forge commands. You can now list issues, create issues, and lots more, yay!

@mreishus
Copy link

mreishus commented Jun 7, 2020

(Removed comment about how I couldn't get this to work).
Make sure you use emacs to save the ~/.authinfo.gpg file - that way it will encrypt it for you. :)

@duchainer
Copy link

A big thanks to you! Been searching this for some time now ^^

@freddie-freeloader
Copy link

freddie-freeloader commented Oct 7, 2020

Thank you!

@hyperfocus1337
Copy link

hyperfocus1337 commented Oct 25, 2020

Am getting Required Gitlab token ("HyperFocus^forge" for "gitlab.com/api/v4") does not exist with an API key with all the permissions set, no idea how to debug as the official docs on the initial setup are quite sparse.

The docs do mention the text below, which suggests only the api permission is needed.

For Gitlab instances api is the only required scope. It gives read and write access to everything. The Gitlab API provides more fine-grained scopes for read-only access, but when any write access at all is required, then it is all or nothing.

Some more docs are available here and here, but have not been able to figure it out so far.

@duchainer
Copy link

duchainer commented Oct 26, 2020

@hyperfocus1337, it doesn't seem like a gitlab token permission problem if forge think it doesn't exist.
Maybe:

  • Check to make sure you wrote your gitlab username correctly
  • Check to make sure your API key was correctly copy pasted without accidental ^@ or similar
  • Make sure to have the login and password in the exact order?

@hyperfocus1337
Copy link

hyperfocus1337 commented Dec 8, 2020

Turns out it was the auth-sources variable that was causing magit forge to misbehave

Default doom Emacs auth-sources
(setq auth-sources '(macos-keychain-generic macos-keychain-internet "/Users/user/.emacs.d/.local/etc/authinfo.gpg" "~/.authinfo.gpg"))

Changed it to
(setq auth-sources '("~/.authinfo.gpg"))

I can successfully pull topics now

@sugat009
Copy link

sugat009 commented Oct 8, 2021

Does this work for github as well? OR can someone show me how to setup for github in doom emacs?

@Azeirah
Copy link
Author

Azeirah commented Oct 10, 2021

Does this work for github as well? OR can someone show me how to setup for github in doom emacs?

I assume it would work mostly the same, but I haven't tried it before.

@Yousuf28
Copy link

Yousuf28 commented Feb 8, 2023

for github user,
I created .authinfo file in home directory and
put following infomation
machine api.github.com login ziggy^forge password 012345abcdef....
where ziggy is the username and 012345... is the token.
and added
(setq auth-sources '("~/.authinfo"))
in config.el file.

@kassick
Copy link

kassick commented May 22, 2024

I had to add machine {{HOST}}api/v4 login {{USER}}^forge secret {{API_KEY}} to authrc for forge to work with my gitlab instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment