Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Created April 23, 2012 19:32
Show Gist options
  • Save bjhomer/2473281 to your computer and use it in GitHub Desktop.
Save bjhomer/2473281 to your computer and use it in GitHub Desktop.
Putting "*.m diff=objc" in <repo_root>/.gitattributes

Running echo "*.m diff=objc" >> .gitattributes in a git repo tells git to use the obj-c diff algorithm when running git diff. For the most part, this isn't much different from the standard diff algorithm. However, it adds one key benefit.

In a unified git diff, added lines are prefixed with a +, and removed lines are prefixed with a -. Unchanged lines are provided for context, and have no prefix. As added context, though, unified diffs have a @@-prefixed line at the beginning of each hunk. Minimally, the @@ lines specify which lines in the file are being changed. It can also contain a label, if git can determine an appropriate label for that section of code.

By default, the label on a hunk context line is the name of the enclosing C function. That works great for C-ish code, but completely misses Obj-C method signatures. As a result, when diffing Obj-C files, the context label is either empty, or falls back to the last C-ish declaration it could find. This is usually entirely useless.

Adding the diff=objc attribute to .m files tells git how to find method headers. As a result, your context lines will now tell you what method the change was in, where previously they were useless.

-@@ -379,7 +379,7 @@
+@@ -379,7 +379,7 @@ - (void)tappedSidebar:(UITapGestureRecognizer *)recognizer {
@@ -379,7 +379,7 @@ - (void)tappedSidebar:(UITapGestureRecognizer *)recognizer {
CGPoint hitPoint = [recognizer locationInView:_scrollView];
NSInteger newSelection = ((isHorizontal ? hitPoint.x : hitPoint.y) / rowHeight);
- if (newSelection > self.imageCount - 1) {
+ if (newSelection > self.imageCount - 1 || self.imageCount == 0) {
self.selectedIndex = -1;
}
else {
@@ -379,7 +379,7 @@
CGPoint hitPoint = [recognizer locationInView:_scrollView];
NSInteger newSelection = ((isHorizontal ? hitPoint.x : hitPoint.y) / rowHeight);
- if (newSelection > self.imageCount - 1) {
+ if (newSelection > self.imageCount - 1 || self.imageCount == 0) {
self.selectedIndex = -1;
}
else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment