summaryrefslogtreecommitdiff
path: root/src/blog/git-plugins.kuht
blob: b26648a8e90e67b9bdb8036e0da6d05440af2dcd (plain)
<import "base.kuht" as "base" />

<head>
	<title>Make Yourself Some Git Plugins</title>
	<meta name="description" content="Git has a plugin system, and it's surprisingly easy to make your own scripts." />
</head>

<body>

<article>

<h1>Make Yourself Some Git Plugins</h1>

<p>You may already be aware of some popular git plugins:</p>
<ul>
	<li><a href="https://github.com/scottgonzalez/recursive-blame">git-recursive-blame</a></li>
	<li><a href="https://github.com/qw3rtman/git-fire">git-fire</a></li>
	<li><a href="https://github.com/jayphelps/git-blame-someone-else">git-blame-someone-else</a></li>
	<li><a href="https://github.com/sobolevn/git-secret">git-secret</a></li>
	<li><a href="https://github.com/nilbuild/git-standup">git-standup</a></li>
	<li><a href="https://github.com/paulirish/git-recent">git-recent</a></li>
	<li><a href="https://github.com/imsky/git-fresh">git-fresh</a></li>
	<li><a href="https://botahamec.dev/cgit/git-autosave">git-autosave</a> (wait, who made that?)</li>
</ul>

<p>
	Many of you likely already know how to implement such a plugin, but I want to
	provide a short tutorial for everyone else.
</p>

<p>
	A command that I often find useful is <code>git publish</code>. If the branch
	doesn't already exist on the remote, it's nice to be able to have an alias for
	<code>git push --set-upstream origin $BRANCH_NAME</code>. Some plugins do this
	automatically, but it's very easy to write, so there's not much reason we
	can't implement it ourselves.
</p>

<pre>
#!/usr/bin/env sh

BRANCH=${git rev-parse --abbrev-ref HEAD}
git push --set-upstream origin $BRANCH
</pre>

<p>
	Now that we have that, we can make it accessible to Git. The easiest way to do
	this is to put the script in your <code>~/.local/bin</code> directory, since
	it's usually already in the <code>PATH</code>. We'll call the full file:
	<code>~/.local/bin/git-publish</code>. We won't add a <code>.sh</code> file
	extension, because the shebang already tells the OS how to run this program.
</p>

<p>
	Then we just need to make the program executable. Run
	<code>chmod +x ~/.local/bin/git-publish</code>, and you're done. We can now
	run it as <code>git publish</code>. When Git sees the <code>publish</code>
	command, it will look through the path to see if an executable called
	<code>git-publish</code> exists, and run that.
</p>

<p>
	See how easy that was? If you want to do something more complicated with Git,
	I recommend reading the
	<a href="https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain">Git Internals</a>
	chapter from <a href="https://git-scm.com/book/en/v2">Pro Git</a>. Git is
	really more of a database than a version control system, and I'm always
	disappointed by the lack of creativity people put into git plugins. Writing a
	sillier project would be too long for this blog post, but it'd be cool to see
	someone make an MMO using Git as the database. The sky is the limit.
</p>