aboutsummaryrefslogtreecommitdiff
path: root/build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2017-04-28 00:30:19 +0200
committerneodarz <neodarz@neodarz.net>2017-04-28 00:30:19 +0200
commit9a88e9ff0385f66e7c565a394908503dc6e916ad (patch)
tree05ea8b356163f06c5fc99c2caf67fa8d3a28d67d /build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html
parentf1965c50670f611ef54f9471490d45a554f7d866 (diff)
downloadmy_new_personal_website-9a88e9ff0385f66e7c565a394908503dc6e916ad.tar.xz
my_new_personal_website-9a88e9ff0385f66e7c565a394908503dc6e916ad.zip
Site updated at 2017-04-28T00:29:42+02:00
source branch was at: f1965c50670f611ef54f9471490d45a554f7d866 Correct a link
Diffstat (limited to 'build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html')
-rw-r--r--build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html73
1 files changed, 73 insertions, 0 deletions
diff --git a/build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html b/build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html
new file mode 100644
index 00000000..88ab872a
--- /dev/null
+++ b/build/blog/2015-12-08-safeguarding-git-repos-against-accidental-rm.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8"/>
+<meta content="pandoc" name="generator"/>
+<meta content="Zhiming Wang" name="author"/>
+<meta content="2015-12-08T00:17:39-08:00" name="date"/>
+<title>Safeguarding git repos against accidental rm</title>
+<link href="/img/apple-touch-icon-152.png" rel="apple-touch-icon-precomposed"/>
+<meta content="#FFFFFF" name="msapplication-TileColor"/>
+<meta content="/img/favicon-144.png" name="msapplication-TileImage"/>
+<meta content="width=device-width, initial-scale=1" name="viewport"/>
+<link href="/css/normalize.min.css" media="all" rel="stylesheet" type="text/css"/>
+<link href="/css/theme.css" media="all" rel="stylesheet" type="text/css"/>
+<link href="/css/highlight.css" media="all" rel="stylesheet" type="text/css"/>
+</head>
+<body>
+<div id="archival-notice">This blog has been archived.<br/>Visit my home page at <a href="https://zhimingwang.org">zhimingwang.org</a>.</div>
+<nav class="nav">
+<a class="nav-icon" href="/" title="Home"><!--blog icon--></a>
+<a class="nav-title" href="/"><!--blog title--></a>
+<a class="nav-author" href="https://github.com/zmwangx" target="_blank"><!--blog author--></a>
+</nav>
+<article class="content">
+<header class="article-header">
+<h1 class="article-title">Safeguarding git repos against accidental rm</h1>
+<div class="article-metadata">
+<time class="article-timestamp" datetime="2015-12-08T00:17:39-08:00">December 8, 2015</time>
+</div>
+</header>
+<p>Everyone who has spent a sizable portion of their life in terminals has experienced that "oh shit" moment: you realize what you've done immediately after you've hit enter, but it's already too late. And needlessly to say, many of those are associated to accidental <code>rm</code>s.</p>
+<p>I just had one of those moments. I was going to delete a subdirectory of <code>~/.config</code>, but hit return prematurely, and the command line ended up being <code>rm -r ~/.config</code>. Imagine the horror one second later. Fortunately I was saved by the read-only objects in <code>.git</code>, which triggered prompts; however, damage was already done, to some extent. I had to reinit the repo and do a hard reset, and a corrupted submodule was in my way (it blocked my attempt of <code>git reset --hard</code>) which I eventually had to completely remove and re-add. In the end everything was recovered (hopefully) and back to normal, but this episode was definitely not great for heart health, which led me to rethink <code>rm</code>.</p>
+<p>I've tried several safer <code>rm</code> solutions before. The first and obvious is to alias <code>rm</code> to <code>rm -i</code>, but having to answer dozens of prompts a day (or more) is agonizing and unproductive. I've also tried trashing, but a nonempty trash can makes me sick, so not for me either. I also used <code>safe-rm</code> for a couple of months, but without supplying my own blacklist (I have none to be blacklisted), I've never hit the default blacklist; apparently I'm not stupid enough to mess in system locations, so this won't really help much. Fortunately though, this time I might have found a very good solution for myself.</p>
+<p>The idea is to protect all git repos. Git repos<a class="footnoteRef" href="#fn1" id="fnref1"><sup>1</sup></a> are among the most valuable assets of programmers, and they have the nice property of not being completely removable without <code>-f</code> or <code>--force</code> (the work tree of a submodule, where <code>.git</code> is a regular file containing the relative path of the git dir, can be removed without <code>--force</code>, but we don't want to damage submodules anyway, so let's not single them out). It's unlikely that we would intend to remove a repo directory without specifying <code>-f</code> or <code>--force</code>, so let's just reject all such <code>rm</code> calls.</p>
+<p>The wrapper is very easy to write. Here's one implementation for Zsh with support for both GNU coreutils and BSD <code>rm</code>.</p>
+<div class="sourceCode"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span class="kw">rm</span> <span class="kw">()</span> <span class="kw">{</span>
+ <span class="kw">setopt</span> localoptions noshwordsplit noksharrays
+ <span class="kw">local</span> <span class="ot">args_backup</span> <span class="ot">force</span> <span class="ot">node</span>
+ <span class="kw">set</span> -A args_backup <span class="ot">$@</span>
+ <span class="kw">while</span> <span class="kw">:</span>; <span class="kw">do</span>
+ <span class="kw">case</span> <span class="ot">$1</span><span class="kw"> in</span>
+ --force<span class="kw">|</span>-*f*<span class="kw">)</span> <span class="ot">force=</span>1 <span class="kw">&amp;&amp;</span> <span class="kw">shift;;</span>
+ --<span class="kw">)</span> <span class="kw">shift</span> <span class="kw">&amp;&amp;</span> <span class="kw">break;;</span>
+ -*<span class="kw">)</span> <span class="kw">shift;;</span>
+ *<span class="kw">)</span> <span class="kw">break;;</span>
+ <span class="kw">esac</span>
+ <span class="kw">done</span>
+ <span class="kw">for</span> node; <span class="kw">do</span>
+ <span class="co"># -f, --force hasn't been specified &amp;&amp; node is a git repo</span>
+ [[ -z <span class="ot">$force</span> <span class="kw">&amp;&amp;</span> -e <span class="ot">$node</span>/.git ]] <span class="kw">&amp;&amp;</span> <span class="kw">{</span>
+ <span class="kw">printf</span> <span class="st">"\e[31m'%s' is a git repo -- won't remove without the -f or --force option\e[0m\n"</span> <span class="ot">$node</span>
+ <span class="kw">return</span> 1
+ <span class="kw">}</span>
+ <span class="kw">done</span>
+ <span class="kw">command</span> <span class="kw">rm</span> <span class="ot">$args_backup</span>
+<span class="kw">}</span></code></pre></div>
+<p>Personally, I stick it into a <a href="https://github.com/zmwangx/prezto/tree/master/modules/rm_guard">Prezto module</a> available from my fork. Hopefully it will serve me well this time round.</p>
+<div class="footnotes">
+<hr/>
+<ol>
+<li id="fn1"><p>In this article, "repo" stands for the work tree of a repo, unless otherwise noted; the actual repo with git objects is referred to as "git dir".<a class="footnotes-backlink" href="#fnref1">↩︎</a></p></li>
+</ol>
+</div>
+</article>
+<hr class="content-separator"/>
+<footer class="footer">
+<span class="rfooter">
+<a class="rss-icon" href="/rss.xml" target="_blank" title="RSS feed"><!--RSS feed icon--></a><a class="atom-icon" href="/atom.xml" target="_blank" title="Atom feed"><!--Atom feed icon--></a><a class="cc-icon" href="https://creativecommons.org/licenses/by/4.0/" target="_blank" title="Released under the Creative Commons Attribution 4.0 International license."><!--CC icon--></a>
+<a href="https://github.com/zmwangx" target="_blank">Zhiming Wang</a>
+</span>
+</footer>
+</body>
+</html>