ruby-on-railsgitherokuword-diff

Running a git shell command on Heroku


I use git to compare strings (I've written why below). It works in development on my machine, but not on Heroku.

`git diff $(echo "hi" | git hash-object -w --stdin) $(echo  "hello" | git hash-object -w --stdin)  --word-diff`

I get the following error:

fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>

Does anyone know how I can get this command working on Heroku? (btw the usage: hint doesn't fix the problem).

Update

Heroku support directed me to this buildpack so that I can manually install git on their servers. The solution is in my answer below.


Solution

  • The solution was:

    Summary

    First try git init in the Heroku console (step 5 & 6), maybe git is already installed. If not:

    1. Install this buildpack
    2. Add a Aptfile (txt) to the root, similar to a Procfile
    3. Add latest git source url to the Aptfile
    4. Deploy
    5. heroku run rails c
    6. `git init` in the console

    In more detail

    1. Heroku directed me to this buildpack. Easy to install. But initially gave me this error, without too much explanation:
    remote: -----> App not compatible with buildpack: https://github.com/heroku/heroku-buildpack-apt.git
    remote:        More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
    
    1. This was because I had not yet added an Aptfile, so do that before deploying because it won't work without. An Aptfile is just a txt file at the root of your rails app, like a Procfile. An empty Aptfile and a successful deployment. Now we have to add git.

    2. Install the git by finding the latest version and copying the link from this folder and adding it to your Aptfile. My Aptfile looks like this:

    https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.25.0.tar.gz
    
    1. Deploy, everything should be working.

    2. Run heroku run rails c

    3. Try the code below again and you'll notice you get a similar error:

    `git diff $(echo "hi" | git hash-object -w --stdin) $(echo  "hello" | git hash-object -w --stdin)  --word-diff`
    => fatal: not a git repository (or any parent up to mount point /)
    => Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
    => fatal: not a git repository (or any parent up to mount point /)
    => Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
    => Not a git repository
    => To compare two paths outside a working tree:
    => usage: git diff [--no-index] <path> <path>
    
    1. But we now have git installed, so just make a repo:
    `git init`
    => "Initialized empty Git repository in /app/.git/\n"
    
    1. Success:
    `git diff $(echo "hi" | git hash-object -w --stdin) $(echo  "hello" | git hash-object -w --stdin)  --word-diff`
    => "diff --git a/45b983be36b73c0788dc9cbcb76cbb80fc7bb057 b/ce013625030ba8dba906f756967f9e9ca394464a\nindex 45b983b..ce01362 100644\n--- a/45b983be36b73c0788dc9cbcb76cbb80fc7bb057\n+++ b/ce013625030ba8dba906f756967f9e9ca394464a\n@@ -1 +1 @@\n[-hi-]{+hello+}\n"
    

    Hope this helps someone else.