1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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">&&</span> <span class="kw">shift;;</span>
--<span class="kw">)</span> <span class="kw">shift</span> <span class="kw">&&</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 && node is a git repo</span>
[[ -z <span class="ot">$force</span> <span class="kw">&&</span> -e <span class="ot">$node</span>/.git ]] <span class="kw">&&</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>
|