In a bash script, git does not recognize it own directory?

So I have written a bash script within Atlassian-Stash for post-receive events. In this script, after a commit has been made, it creates a codecollaborator code review. To create a code review, it needs commit title, commit user and git SHA for any changes and uploading the changes to the code review. To get these informations, I clone’d the directory to –depth=1 (even without –depth=1) and work with git log (options).

The problem I am seeing is that if I run the script manually, it works just fine. However, if it runs after a commit has been made, it errors out after it clones the directory saying it is not a git directory. If I cd into the directory after the script exits, I am able to run git log (and other git commands).

  • How can I connect to my own Git server in SourceTree?
  • How to get physical files from git stash repository?
  • How do I setup Git version control on Jenkins (on Windows)
  • Atlassian Stash says that the second pull request merged remotely
  • How do I set up a git repo to be used by multiple users?
  • Jenkins CI Post-build Actions - How to push to Stash
  • Things I tried to troubleshoot are
    1. Permissions issues (running it as root), so I am not seeing any permissions issues.
    2. troubleshooting it with bash -xv and until that point everything looks good.
    3. I also put it status checks with $?
    4. I tried to move .git to git-backup, wait 3 seconds and moved it back, still the same issue.
    5. I ran ls -ltra to make sure that it has all the files and .git directory.

    Now, I am out of options. Has anyone ran into this kind of problem before?

    Anyone know where I might be doing something wrong or missing something?

    I tried to be as descriptive as possible, if the question does not make sense or need a sample script, please let me know.

    Adding the script and its error output below.

    #!/bin/bash -xv
    
    CCollabExe='/usr/local/bin/ccollab'
    CCollabUrl='--url http://***:8080'
    CCollabUser='--user ******'
    CCollabPassword='--password ******'
    CCollabConnection="${CCollabExe} ${CCollabUrl} ${CCollabUser} ${CCollabPassword}"
    CCollabStuff='/home/stash/repositories/tmp'
    CloneDir="${CCollabStuff}/ClonnedDir"
    StashUser='******'
    StashPass='******'
    RepoURLlinkGit="http://${StashUser}:${StashPass}@******:7990/scm/t/test1.git"
    
    unset SSH_ASKPASS
    
    # Test function to check if a varibale is empty
    CheckIfVarEmpty () {
      local Variable="$1"
      if [[ -z ${Variable} ]] ; then
         echo "Variable $1 '\${Variable}' is empty, exiting"
         echo "Lets try to go back in the git dir" && cd ${CloneDir} && git log -10
         cd /root && cd ${CloneDir}
         [[ -d .git ]] && cp -rp .git git-backup && rm -rf .git && echo "sleeping 3" && sleep 3 && mv git-backup .git
         git log -10
         exit 0
      fi
    }
    
    #Create a new CCollab temp dir, clone the directory and get commit title, user and SHA info
       rm -rf ${CCollabStuff} && mkdir ${CCollabStuff} && cd ${CCollabStuff}
       git clone ${RepoURLlinkGit} ${CloneDir}
       cd ${CloneDir}
    

    # below is where its erroring out.

       CommitTitle=$(git log  --pretty=format:"%s" -1)
       CheckIfVarEmpty ${CommitTitle}
       CommitUser=$(git log  --pretty=format:"%an" -1)
       CheckIfVarEmpty ${CommitUser}
       CommitSHA=$(git log  --pretty=format:"%h" -2)
       CheckIfVarEmpty ${CommitSHA}
       CommitSHA1=$(echo $CommitSHA | awk -F' ' '{ print $1 }')
       CommitSHA2=$(echo $CommitSHA | awk -F' ' '{ print $2 }')
       echo "=========="
    

    Error out is:

    remote:   rm -rf ${CCollabStuff} && mkdir ${CCollabStuff} && cd ${CCollabStuff}
    remote: + rm -rf /home/stash/repositories/tmp
    remote: + mkdir /home/stash/repositories/tmp
    remote: + cd /home/stash/repositories/tmp
    remote:   git clone ${RepoURLlinkGit} ${CloneDir}
    remote: + git clone http://******:******@******:7990/scm/t/test1.git /home/stash/repositories/tmp/ClonnedDir
    remote: Cloning into '/home/stash/repositories/tmp/ClonnedDir'...
    remote:   cd ${CloneDir}
    remote: + cd /home/stash/repositories/tmp/ClonnedDir
    remote:   CommitTitle=$(git log  --pretty=format:"%s" -1)
    remote: git log  --pretty=format:"%s" -1
    remote: ++ git log --pretty=format:%s -1
    remote: fatal: Not a git repository: '.'
    

  • How to clone Hibernate sources using Egit (Eclipse)?
  • Why in Windows might attempting to delete a file twice work?
  • why doen't conflict diffs load on Filemerge, when other diffs do?
  • How to remove a dangling commit from GitHub?
  • how to merge to a local branch after checking out to a previous commit
  • Remove file from Version Control in IntelliJ IDEA
  • One Solution collect form web for “In a bash script, git does not recognize it own directory?”

    I know nothing about Atlassian but it’s clear from the error output that you’re tripping over one of the hook traps I noted in an answer I can’t find now:

    • In a git hook, the environment variable GIT_DIR is set (to . in --bare repos, to .git in non-bare ones). This is valid only until you cd to some other directory, often in a sub-process run from the hook script that has no idea that $GIT_DIR is pointing off to some now-inappropriate place.

    (The git clone step works because it is not looking for a git directory, it’s just creating a new one.)

    The quick and easy fix is unset GIT_DIR.

    Git Baby is a git and github fan, let's start git clone.