[1+1=2]

OneAndOneIs2

« A year of Convict ConditioningAmerica loses the Internet? »

Mon, Jun 09, 2014

[Icon][Icon]For the sin of moving & editing in one commit

• Post categories: Omni, Technology, Programming, Helpful

A good rule of thumb for version control is to have each commit reflect a single operation. Nowhere is the failure to adhere to this rule more annoying than when somebody moves a block of code *and* edits it in a single commit: The move makes the whole block show in the diff, so you can't see the small edit as well.

That is:

$ cat <<EOF> oldfile
heredoc> one
heredoc> two
heredoc> three
heredoc> four
heredoc> five
heredoc> six
heredoc> seven
heredoc> eight
heredoc> EOF

git add oldfile; git commit

$ git mv oldfile newfile
$ vi newfile
$ git diff
diff --git newfile newfile
index b00a0f1..9e9d5bf 100644
--- newfile
+++ newfile
@@ -1,7 +1,6 @@
one
two
three
-four
five
six
seven

$ git add newfile
$ git commit

We now have this diff for our commit:
new file mode 100644
index 0000000..9e9d5bf
--- /dev/null
+++ newfile
@@ -0,0 +1,7 @@
+one
+two
+three
+five
+six
+seven
+eight
diff --git oldfile oldfile
deleted file mode 100644
index b00a0f1..0000000
--- oldfile
+++ /dev/null
@@ -1,8 +0,0 @@
-one
-two
-three
-four
-five
-six
-seven
-eight

and it's not easy to see from this that when moving the file, we also deleted a line.

What we need to be able to do is compare the oldfile and the newfile. We need a function that will take:
$ git-compare [branch/sha/tag/whatever] [oldfile] [newfile]
and DTRT.

So here's the simplest way I could think of to do it, which should work in bash or zsh:
git-compare () {
diff -u <(git show $1^:$2) <(git show $1:$3)
}

Which gives us:
$ git-compare master oldfile newfile
--- /proc/self/fd/12 2014-06-09 11:58:01.442468718 +0100
+++ /proc/self/fd/14 2014-06-09 11:58:01.442468718 +0100
@@ -1,7 +1,6 @@
one
two
three
-four
five
six
seven

which shows us what we want to know: Somebody did indeed move AND edit the file. The monster!


No feedback yet

 

[Links][icon] My links

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


August 2017
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  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      

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution CMS