Skip to content

Instantly share code, notes, and snippets.

@alphapapa
Last active April 5, 2020 10:18
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alphapapa/ca1ea75675a826e572bced86f69f51b6 to your computer and use it in GitHub Desktop.
Save alphapapa/ca1ea75675a826e572bced86f69f51b6 to your computer and use it in GitHub Desktop.
Run a standalone Magit editor!
# Please see the script's new home: https://github.com/alphapapa/magit.sh
@priyadarshan
Copy link

Wonderful, thanks!

@c02y
Copy link

c02y commented Aug 17, 2019

Don't know whether you have tested it, but it doesn't work for me. (bash: GNU bash, version 5.0.7(1)-release)

I tweaked it a little bit. Now it works great.

  1. -h doesn't work, since getopt doesn't handle -h/--help
  2. usage function doesn't work, use cat instead of echo
  3. without specifying git repo, open magit and it will ask for repo (the original action)
  4. with specifying git repo, open magit in that git repo directly
  5. I'm using maxdepth=3 since I'm using Spacemacs. (change it back to 1 if you are not using Spacemacs)
#!/bin/bash

# Run a standalone Magit editor!  To improve startup speed, this
# script ignores the user's Emacs init files and only loads the Emacs
# libraries Magit requires.

# Note that this does NOT install any packages.  Magit and its
# dependencies must already be installed in ~/.emacs.d.

dependencies=(magit async dash with-editor git-commit transient)

function load_paths {
	# Echo the "-L PATH" arguments for Emacs.  Since multiple versions
	# of a package could be installed, and we want the latest one, we
	# sort them and take the top one.
	for package in "$@"; do
		find ~/.emacs.d/elpa -maxdepth 3 -type d -iname "$package-2*" \
			| sort -r | head -n1 | \
			while read path; do
				printf -- '-L %q ' "$path"
			done
	done
}

function usage {
	cat <<EOF
It's Magit!
Options:
  -h, --help  This.
  -x, --no-x  Display in terminal instead of in a GUI window.
EOF
}

# * Args

args=$(getopt -n "$0" -o hx -l help,no-x -- "$@") || { usage; exit 1; }
eval set -- "$args"

while true; do
	case "$1" in
		-h|--help)
			usage
			exit ;;
		-x|--no-x)
			gui="-nw" ;;
		--)
			# Remaining args (required; do not remove)
			shift
			rest=("$@")
			break ;;
	esac
	shift
done

# * Main
if [ -z "$1" ]; then
	emacs -q $gui \
		  $(load_paths "${dependencies[@]}") \
		  -l magit -f magit-status \
		  --eval "(local-set-key \"q\" #'kill-emacs)" \
		  -f delete-other-windows
else
	emacs -q $gui \
		  $(load_paths "${dependencies[@]}") -l magit \
 		  --eval "(magit-status \"$1\")" \
		  --eval "(local-set-key \"q\" #'kill-emacs)" \
		  -f delete-other-windows
fi

@alphapapa
Copy link
Author

@c02y Thanks, please see the latest revision.

@tasmo
Copy link

tasmo commented Sep 30, 2019

Also consider to change the bangline. On NixOS and other systems not using the standard path for bash it wont work.

Instead of

#!/bin/bash

it should be

#!/usr/bin/env bash

@dprae
Copy link

dprae commented Apr 2, 2020

I get this:

command-line-1: Cannot open load file: No such file or directory, magit

@c02y
Copy link

c02y commented Apr 3, 2020

@dprae show the whole process, how did you get this error?

@dprae
Copy link

dprae commented Apr 5, 2020

@c02y, sorry for the late response, I figured out what the problem was. My packages weren't directly in elpa/, but in elpa/28.0/develop, I modified the script and it works perfectly.

@alphapapa
Copy link
Author

FYI, I've put the script in a repo of its own: https://github.com/alphapapa/magit.sh

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