Check before you commit

Issue #7

Original post https://medium.com/@onmyway133/check-before-you-commit-5a7601cffc87


We usually have some experiment code that we don’t want they to step into our commit. I usually mark my experiment with // but sometimes forget to unstage that
Starting with 2.9, Git has improvement on its commit hook which makes it globally using hooksPath

Create pre-commit file

Create a file called pre-commit, and place it into, for example /Users/khoa/hooks

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
#!/bin/sh

# https://appventure.me/2016/04/04/prevent-accidental-test-code-commits/

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# The special marker tag to mark things which we still need to change
marker="<TEST>"

# Redirect output to stderr.
exec 1>&2

if test $(git diff --cached -z $against | grep $marker | wc -c) != 0
then
cat <<\EOF
Error: Still has invalid debug markers in code:
EOF
echo `git diff --cached -z $against -G $marker`
exit 1
fi

Apply the hook

In your project, run git config core.hooksPath /Users/khoa/hooks
That’s it. Whenever you commit a file with that pattern, it won’t let you commit

How to use

Try

1
2
// <TEST>
UserManager.shared.isActive = true

and git commit -m "my commit message" will assert with Error: Still has invalid debug markers in code:

Reference

Markdown editor

Issue #6

I like writing with markdown, it gives me comfortable experience with complete control over what I want to write.

I recommend vmd which renders exactly as GitHub. vmd is for rendering only, you need an editor to write, I use Sublime Text because it opens very fast

I also recommend using spectacle to easily split and organize windows

vmd

Primary key in Realm

Issue #4

Realm is great. But without primary key, it will duplicate the record, like https://github.com/realm/realm-java/issues/2730, http://stackoverflow.com/questions/32322460/should-i-define-the-primary-key-for-each-entity-in-realm, … So to force ourselves into the good habit of declaring primary key, we can leverage Swift protocol

Create primary constrain protocol like this

1
2
3
4
protocol PrimaryKeyAware {
var id: Int { get }
static func primaryKey() -> String?
}

and conform it in out Realm object

1
2
3
4
5
6
7
8
9
10
class Profile: Object, PrimaryKeyAware {

dynamic var firstName: String = ""
dynamic var lastName: String = ""
dynamic var id: Int = 0

override static func primaryKey() -> String? {
return "id"
}
}

This way, when using that object in out RealmStorage, we are safe to say that it has a primary key

1
2
3
4
5
6
7
8
9
10
11
12
13
class RealmStorage<T: Object> where T: PrimaryKeyAware {
let realm: Realm

init(realm: Realm = RealmProvider.realm()) {
self.realm = realm
}

func save(_ objects: [T]) {
try? realm.write {
realm.add(objects, update: true)
}
}
}

The usage is like this

1
2
3
let profile = Profile()
let storage = RealmStorage<Profile>()
storage.save([profile])

Configuration closure in Swift

Issue #2

When I was reading through Swinject, I found something interesting https://github.com/Swinject/Swinject/blob/master/Sources/Container.swift

1
2
3
4
public convenience init(parent: Container? = nil, registeringClosure: (Container) -> Void) {
self.init(parent: parent)
registeringClosure(self)
}

The init has a closure that makes configuration easy, much like a Builder pattern. So I think we can learn from that and make a Configurable protocol

1
2
3
4
5
6
7
8
9
10
protocol Configurable: class {}

extension Configurable {
func config(block: (Self) -> Void) -> Self {
block(self)
return self
}
}

extension NSObject : Configurable {}

With this, we can init some class with less hassle

1
2
3
4
let view = UIView().config {
$0.backgroundColor = .white
$0.layer.cornerRadius = 2
}

Hello world, again

Issue #1

I’ve used Wordpress, then moved to GitHub Pages with Jekyll, Octopress, Hexo, Hugo. You can view my page here http://fantageek.com/. It was good with all the custom themes and Disqus

But then I was a bit lazy with all the commands generate, commit, deploy, it hinders me from writing, so I moved to Medium.

The only thing I like about Medium is its discovery, your posts have high chanced of finding and viewing by people. What’s the point of writing if no one read it? But then I miss all the awesome markdown features of GitHub Pages. Medium is easy to use, but it seems it’s not for hackers, and I find it really uncomfortable when adding code block and headings. Medium also lists my comments as stories, which is kind of 😲

I like to write fast, and with good comments system, and I love Markdown.

I like GitHub. I use GitHub for my notes, so I think I will use it for my blog as well. Hope all these GitHub convenience will encourage me to write more often. This will, of course, be less discoverable by people. So if you by any chance visit this blog, ohayou from me 👋

Updated at 2020-12-17 17:12:38