diff --git a/README.md b/README.md index 98413e0..a8c2bdb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 647ef46..2656513 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -34,4 +34,4 @@ "moment": "^2.9.0", "ul": "^1.1.0" } -} \ No newline at end of file +} diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit new file mode 100755 index 0000000..88a0875 --- /dev/null +++ b/scripts/init-git-post-commit @@ -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