Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Azeirah
Last active November 23, 2023 07:07
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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!

@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.

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