Skip to content

Instantly share code, notes, and snippets.

@alphapapa alphapapa/magit.sh
Last active Sep 15, 2019

Embed
What would you like to do?
Run a standalone Magit editor!
#!/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 1 -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!
magit.sh [OPTIONS] [PATH]
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
[[ -d $1 ]] && cd "$1"
emacs -q $gui \
$(load_paths "${dependencies[@]}") \
-l magit -f magit-status \
--eval "(local-set-key \"q\" #'kill-emacs)" \
-f delete-other-windows
@priyadarshan

This comment has been minimized.

Copy link

commented Aug 17, 2019

Wonderful, thanks!

@c02y

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

commented Aug 18, 2019

@c02y Thanks, please see the latest revision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.