Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git hooks #6

Merged
merged 12 commits into from
Feb 11, 2015
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,26 @@ $ npm install -g git-stats
```

### Catching the `git commit` command
Would you like to catch and automatically store the commits when you do `git commit`?
Would you like to catch and automatically store the commits when you do `git commit`? If so, try one of the following solutions.

If so, put the following lines in your `~/.bashrc` (or `~/.bash_profile` on OS X) file:
#### Using `git` hooks
The way I recommend to track your git commits is to use git hooks. Run the following command to initialize the `post-commit` git hook.

```sh
# Using curl
curl -s https://raw.githubusercontent.com/IonicaBizau/git-stats/master/scripts/init-git-post-commit | bash

# ...or wget
wget -qO- https://raw.githubusercontent.com/IonicaBizau/git-stats/master/scripts/init-git-post-commit | bash
```

Then, you have to run `git init` into your existing git repositories from your local machine (that's because the `post-commit` should be updated). This
step will not be needed after clonning a repository (the git hooks will be added automatically from `~/.git-templates`).

#### Overriding the `git` command
One of the solutions is becoming a mad scientist, overriding the `git` command with a function. However, this may not work for you if you're using `zsh`.

Add the following lines in your `~/.bashrc` (or `~/.bash_profile` on OS X) file:

```sh
# Override the Git command
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-stats",
"version": "1.0.1",
"version": "1.1.0",
"description": "A GitHub-like contributions calendar, but locally, with all your git commits.",
"main": "lib/index.js",
"bin": {
Expand Down Expand Up @@ -34,4 +34,4 @@
"moment": "^2.9.0",
"ul": "^1.1.0"
}
}
}
21 changes: 21 additions & 0 deletions scripts/init-git-post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

git_templates_dir="$HOME/.git-templates"
git_hooks_dir="$git_templates_dir/hooks"
post_commit_path="$git_hooks_dir/post-commit"

git config --global init.templatedir $git_templates_dir
mkdir -p $git_hooks_dir

cat << EOF > $post_commit_path
#!/bin/sh

# Copy last commit hash to clipboard on commit
commit_hash=\`git rev-parse HEAD\`
repo_url=\`git config --get remote.origin.url\`
commit_date=\`git log -1 --format=%cd\`
commit_data="\"{ \"date\": \"\$commit_date\", \"url\": \"\$repo_url\", \"hash\": \"\$commit_hash\" }\""
git-stats --record "\$commit_data"
EOF

chmod +x $post_commit_path