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

Catching git-commit from zsh #1

Closed
atuttle opened this issue Feb 9, 2015 · 31 comments · Fixed by #6
Closed

Catching git-commit from zsh #1

atuttle opened this issue Feb 9, 2015 · 31 comments · Fixed by #6
Assignees
Labels

Comments

@atuttle
Copy link

atuttle commented Feb 9, 2015

After adding your sample script for catching git commit to my .zshrc file:

$ git status
bash: -c: line 24: syntax error near unexpected token `status'
bash: -c: line 24: `} status'

So for now I'll have to live without it. Would be nice if there was an alternative, for zsh users though. :)

@IonicaBizau IonicaBizau added the bug label Feb 9, 2015
@IonicaBizau
Copy link
Owner

What platform? I only tested this on Ubuntu 14.10 and I have no device to test it on OS X... 😢

Since it's a syntax error, something should be in the override function, not being related to zsh (tested it inside of a zsh session and it works fine).

Would be nice to output the $cmdToRun variable before running it:

...
  echo $cmdToRun # Add this and post the output in the issue
  bash -c "$cmdToRun"
...

@atuttle
Copy link
Author

atuttle commented Feb 9, 2015

I'm on OSX. I added echo $cmdToRun right before bash -c $cmdToRun, but that doesn't seem to fix it. It's probably not helping that I'm using Oh-My-Zsh and I have a git command in my prompt (the green area):

image

If I ignore the error embedded in my prompt, and run a git status I see this:

image

@IonicaBizau
Copy link
Owner

Oh, it seems that when it tries to run git it gets the overridden value (the function we defined). 😮

Can you check if the which git returns the git function we defined?

Maybe we can use the command or alias commands to make this process better.

@atuttle
Copy link
Author

atuttle commented Feb 9, 2015

Yes, which git does show the method we're creating:

image

@IonicaBizau
Copy link
Owner

OK, so let's try the command command:

# My own Git
git() {
  cmd=$1
  shift
  extra=""

  quoted_args=""
  whitespace="[[:space:]]"
  for i in "$@"
  do
      quoted_args="$quoted_args \"$i\""
  done

  cmdToRun="git "$cmd" $quoted_args"
  cmdToRun=`echo $cmdToRun | sed -e 's/^ *//' -e 's/ *$//'`
  bash -c "$cmdToRun"
  if [ $? -eq 0 ]; then
    # Commit stats
    if [ "$cmd" == "commit" ]; then
      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"
    fi
  fi
}

@atuttle
Copy link
Author

atuttle commented Feb 9, 2015

Better... still not quite there.

image

@IonicaBizau
Copy link
Owner

Whoups, in the previous code I didn't use command.

So, let's try it (this will also output the command that is run by command):

git() {
  cmd=$1
  shift
  extra=""

  quoted_args=""
  whitespace="[[:space:]]"
  for i in "$@"
  do
      quoted_args="$quoted_args \"$i\""
  done

  cmdToRun="git "$cmd" $quoted_args"
  cmdToRun=`echo $cmdToRun | sed -e 's/^ *//' -e 's/ *$//'`
  echo ">>> $cmdToRun"
  command $cmdToRun
  if [ $? -eq 0 ]; then
    # Commit stats
    if [ "$cmd" == "commit" ]; then
      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"
    fi
  fi
}

@atuttle
Copy link
Author

atuttle commented Feb 9, 2015

Two steps forward, one step back: (I did think it was a little odd that you mentioned it above the code but I couldn't find it in the code ;))

image

@IonicaBizau
Copy link
Owner

OK! Let's try alias:

do_git() {
  cmd=$1
  shift
  extra=""

  quoted_args=""
  whitespace="[[:space:]]"
  for i in "$@"
  do
      quoted_args="$quoted_args \"$i\""
  done

  cmdToRun="git "$cmd" $quoted_args"
  cmdToRun=`echo $cmdToRun | sed -e 's/^ *//' -e 's/ *$//'`
  echo ">>> $cmdToRun"
  bash -c $cmdToRun
  if [ $? -eq 0 ]; then
    # Commit stats
    if [ "$cmd" == "commit" ]; then
      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"
    fi
  fi
}

alias git="do_git"

@atuttle
Copy link
Author

atuttle commented Feb 9, 2015

image

@IonicaBizau
Copy link
Owner

This is called screenshare via GitHub comments. 😄 The below code works fine on Linux.

git() {
  cmd=$1
  shift
  extra=""

  quoted_args=""
  whitespace="[[:space:]]"
  for i in "$@"
  do
      quoted_args="$quoted_args \"$i\""
  done

  cmdToRun="command git "$cmd" $quoted_args"
  cmdToRun=`echo $cmdToRun | sed -e 's/^ *//' -e 's/ *$//'`
  echo ">>> $cmdToRun"
  $cmdToRun
  echo $?
  if [ $? -eq 0 ]; then
    # Commit stats
    if [ "$cmd" == "commit" ]; then
      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"
    fi
  fi
}

@atuttle
Copy link
Author

atuttle commented Feb 9, 2015

Unfortunately still not working:

image

I may have to give up hope on this for now, and just try to remember to do an import before I want to view the stats.

@IonicaBizau
Copy link
Owner

Maybe some OS X geeks will see this and come with a fix! 😄

@cwebster2
Copy link

This isn't an OS X bug. This same issue exists using zsh on linux (tested with zsh 5.0.5 on linux and OS X). Your function works properly on OS X with bash (tested with bash 3.2.53 on OS X)

@IonicaBizau IonicaBizau removed the OS X label Feb 10, 2015
@IonicaBizau
Copy link
Owner

I couldn't reproduce it in zsh, but anyway I'm removing the OS X label. This will probably be fixed via #4.

@IonicaBizau IonicaBizau self-assigned this Feb 10, 2015
@IonicaBizau IonicaBizau mentioned this issue Feb 10, 2015
@IonicaBizau
Copy link
Owner

Run this script on your machine. It will add the post-commit hook.

Then do git init in all your repositories before committing anything.

BTW, does the importer tool work on Mac?

@revolter
Copy link

@IonicaBizau, It does work on Mac, but for some reason, it only imports a couple of the commits. Is there a restriction limit set by default?

@IonicaBizau
Copy link
Owner

@revolt666 The git-stats-importer tool imports only your commits, it filters them based on the email address you have set.

$ git config user.email

This will output the email address that git-stats-importer uses. No other commits are imported.

@revolter
Copy link

Oh, forgot about that. Can't it get all the commits? Because I'm the only developer but I forgot (or didn't know how) to change the email for a couple of months, and I would be more interested for a general calendar, knowing that the project was being developed by one person at a time.

@IonicaBizau
Copy link
Owner

@revolt666 Sure, if you hack it! 😏

You just need to remove this property.

So:

  • clone the git-stats-importer
  • run npm install there
  • go to the repository you want to import
  • run ~/path/to/git-stats-importer/bin/git-stats-importer and it should work

@revolter
Copy link

@IonicaBizau, Awesome, I will try it later on. Thanks for the detailed guide too 😄

@rambo-panda
Copy link

mac 10.10.2
zsh
have The same problem

@IonicaBizau
Copy link
Owner

@rambo-panda Even using the git hooks? 😮

@IonicaBizau
Copy link
Owner

@atuttle Did you try the git hooks version?

@atuttle
Copy link
Author

atuttle commented Feb 10, 2015

Not yet, swamped at work.

Adam

On Tue, Feb 10, 2015 at 12:21 PM, Ionică Bizău notifications@github.com
wrote:

@atuttle https://github.com/atuttle Did you try the git hooks version?


Reply to this email directly or view it on GitHub
#1 (comment).

@atuttle
Copy link
Author

atuttle commented Feb 11, 2015

I've just had a chance to try the script. It doesn't cause any errors, which is good, but it doesn't seem to capture my commits either.

I've run it, and it did create ~/.git-templates/hooks/post-commit, and I did a git init in the root of one of my existing repositories, but subsequent commits didn't seem to take. I added a simple echo line to the global post-commit hook, and it doesn't appear to be running.

@IonicaBizau
Copy link
Owner

I added a simple echo line to the global post-commit hook, and it doesn't appear to be running.

Add it in ./.git/hooks/post-commit script. If you edit the global hook you need to do: rm -rf .git/hooks && git init.

Hmmm, it should work. I tested it on OS X Yosemite in virtual machine (also in zsh).

Can you confirm that at least the git-stats-importer is working properly?

@atuttle
Copy link
Author

atuttle commented Feb 12, 2015

git-stats-importer has been working for me.

@IonicaBizau
Copy link
Owner

The beginning of good news. 😄

Run the following commands, exactly like below.

$ mkdir foo
$ cd foo
$ git init
$ git-stats # Store the total number of commits
$ echo "foo" > foo
$ git remote add foo
$ git add .
$ git commit -m 'initial'
$ git-stats # Check if the total number is with one more

If this doesn't work in zsh, check if it work out of zsh.

@atuttle
Copy link
Author

atuttle commented Feb 12, 2015

I think I was missing the rm -rf .git/hooks step before. I did a git init in one of my repos but that didn't update the hooks. Seems to be working now.

@IonicaBizau
Copy link
Owner

Cooool! 👍

git init does update the hooks (at least on Linux, but I tested it on OS X Yosemite in a virtual machine), but it doesn't override them. So, if you already have post-commit hook in your repo, git init will not bring the one from ~/.git-templates/hooks/post-commit.

IonicaBizau pushed a commit that referenced this issue Oct 14, 2020
Fix Arbitary Code Execution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants