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

Error: fatal: Not a git repository #170

Closed
tnguyen14 opened this issue May 15, 2017 · 23 comments
Closed

Error: fatal: Not a git repository #170

tnguyen14 opened this issue May 15, 2017 · 23 comments

Comments

@tnguyen14
Copy link

When trying to use lint-staged and husky in a project where the actual node.js project is a child directory of the git repo (as shown below):

project
  └─ node-app
       └─ package.json

I get the following error:

:; git commit                                                                                                                  

> husky - npm run -s precommit

Error: fatal: Not a git repository: '.git'

    at ChildProcess.<anonymous> (/home/tri/project/node-app/node_modules/staged-git-files/index.js:85:19)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:501:12)
/home/tri/project/node-app/node_modules/lint-staged/src/index.js:44
            files.forEach((file) => {
                 ^

TypeError: Cannot read property 'forEach' of undefined
    at sgf (/home/tri/project/node-app/node_modules/lint-staged/src/index.js:44:18)
    at /home/tri/project/node-app/node_modules/staged-git-files/index.js:13:13
    at /home/tri/project/node-app/node_modules/staged-git-files/index.js:38:13
    at ChildProcess.<anonymous> (/home/tri/project/node-app/node_modules/staged-git-files/index.js:88:9)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)

When running npm run precommit, I see a lot of these errors

:; npm run precommit

> node-app@1.0.0 precommit /home/tri/project/node-app
> lint-staged

 ❯ Running tasks for *.js
   ✖ prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write
     → /home/tri/prooject/node-app/node-app/Link.react.js
     git add
🚫 prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write  found some errors. Please fix them and try committing again.
Unable to read file:
Error: ENOENT: no such file or directory, open ''
Unable to read file: /home/tri/project/node-app/node-app/test/nightwatch/user.js
Error: ENOENT: no such file or directory, open '/home/tri/project/node-app/node-app/test/nightwatch/user.js'
Unable to read file: /home/tri/project/node-app/node-app/test/nightwatch/universalSearch.js
@okonet
Copy link
Collaborator

okonet commented May 16, 2017

Did you see https://github.com/okonet/lint-staged#working-from-a-subdirectory? What's your config?

@tnguyen14
Copy link
Author

Ah, I think I put that gitDir config as sibling to lint-staged, not as a property of. My bad. Thanks for your help.

@tnguyen14
Copy link
Author

FWIW, I'm still getting this error, even with the gitDir config.

:; git commit

> husky - npm run -s precommit

 ❯ Running tasks for *.js
   ✖ prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write
     → /home/tri/project/node-app/client/js/App.js/home/tri/project/node-app/client/js/App.js 429ms
     git add
 ↓ Running tasks for gitDir [skipped]
   → No staged files match gitDir
🚫 prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write  found some errors. Please fix them and try committing again.
Unable to read file:
Error: ENOENT: no such file or directory, open ''


/home/tri/project/node-app/client/js/App.js 429ms


@tnguyen14 tnguyen14 reopened this May 16, 2017
@okonet
Copy link
Collaborator

okonet commented May 16, 2017

Try calling prettier as npm script

@okonet
Copy link
Collaborator

okonet commented May 16, 2017

There is an open issue re this

@okonet okonet closed this as completed May 16, 2017
@tnguyen14
Copy link
Author

What is the open issue?

@tnguyen14
Copy link
Author

Could you also clarify on what you mean by "try calling prettier as npm script"?

@okonet
Copy link
Collaborator

okonet commented May 17, 2017

#162

You can wait till this is merged or update you config like it's said in the linked issue to this PR

@maxime1992
Copy link

Hi ! I do have the exact same problem : Trying to setup prettier on an (angular) Typescript project.

{
  "scripts": {
    "prettier:base": "yarn run prettier -- --parser typescript --single-quote --trailing-comma es5",
    "prettier:precommit": "yarn run prettier:base -- --write",
    "precommit": "lint-staged"
  },
  "gitDir": "../",
  "lint-staged": {
    "*.ts": [
      "yarn run prettier:precommit",
      "git add"
    ]
  },
  ...
}

Did you manage to solve this @tnguyen14 ?

@okonet
Copy link
Collaborator

okonet commented Jun 2, 2017

@maxime1992
Copy link

@okonet I've added the gitDir param in my package. Look this line.

Is the problem coming from a missing "linters" property ? I don't really understand why I would need that and how to use it. Maybe I'm missing some context here (https://github.com/okonet/lint-staged#working-from-a-subdirectory).

Can you help me figure that out please ?

@okonet
Copy link
Collaborator

okonet commented Jun 2, 2017

It's all in the readme: https://github.com/okonet/lint-staged#advanced-config-format

@MJ111
Copy link

MJ111 commented Jun 9, 2017

@maxime1992
You have to insert gitDir as property of lint-staged. like this.

{
  "lint-staged": {
    "gitDir": "../",
    "*.ts": [
      "yarn run prettier:precommit",
      "git add"
    ]
  },
  ...
}

@okonet
Copy link
Collaborator

okonet commented Jun 9, 2017

@MJ111 this won't work. You need to use advanced config with linters key. See README

@MJ111
Copy link

MJ111 commented Jun 9, 2017

oh. I didn't know about that. thanks

@okonet
Copy link
Collaborator

okonet commented Jun 9, 2017

That's why I encourage people to read the README when something isn't working :)

@kvsur
Copy link

kvsur commented Dec 18, 2017

you should write like this:

"lint-staged": {
    "gitDir": "../",
    "linters": {
      "**/*.{js,jsx}": "lint-staged:js",
      "**/*.less": "stylelint --syntax less"
    }
  }

this project will check if 'gitDir' option first, then 'linters'

@yiochen
Copy link

yiochen commented Mar 17, 2018

I am still getting the error

husky > npm run -s precommit (node v8.5.0)

 ↓ Running tasks for *.{js,jsx} [skipped]
   → No staged files match *.{js,jsx}
husky > npm run -s postcommit (node v8.5.0)

fatal: Not a git repository: '.git'

husky > post-commit hook failed (add --no-verify to bypass)

This is my package.json

"precommit": "lint-staged",
"postcommit": "git update-index --again"
...
"lint-staged": {
    "**/*.{js,jsx}": ["eslint --fix", "git add"]
}

The package.json is not in the .git root, because the front end code is inside a sub folder. Outside of that is Java code, so it doesn't make sense to create a package.json in the root level.

I also tried the old syntax with "gitDir", it gave me deprecation warning, and then still throw the error for Not a git repository: '.git'

@sudo-suhas
Copy link
Collaborator

@yiochen Could you please share the debug output? You can enable it by changing your precommit script to lint-staged --debug.

@yiochen
Copy link

yiochen commented Mar 18, 2018

@sudo-suhas looks like the precommit passed. It was the postcommit that threw the error. I checked my .git folder, and the hooks are installed correctly.
Below is my --debug output

Running lint-staged with the following config:
{
  gitDir: '../',
  linters: {
    '*.{js,jsx}': [
      'eslint --fix',
      'git add'
    ]
  },
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  ignore: [],
  subTaskConcurrency: 1,
  renderer: 'verbose'
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `/Users/yiouchen/Documents/workspace/project/` +1ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'UIModules/yarn.lock', 'UIModules/package.json' ] +104ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +111ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks { pattern: '*.{js,jsx}',
  lint-staged:gen-tasks   commands: [ 'eslint --fix', 'git add' ],
  lint-staged:gen-tasks   fileList: [] } +20ms
Running tasks for *.{js,jsx} [started]
Running tasks for *.{js,jsx} [skipped]
→ No staged files match *.{js,jsx}
  lint-staged linters were executed successfully! +140ms
husky > npm run -s postcommit (node v8.5.0)


> veeva-ui-modules@ postcommit /Users/yiouchen/Documents/workspace/vault/UIModules
> git update-index --again

fatal: Not a git repository: '.git'

@malixsys
Copy link

Mine works:

// Sub directory package.json
  {
  "lint-staged": {
    "linters": {
      "*.{js,jsx}": "eslint --quiet --cache"
    }
  },
  "scripts": {
    "pre-commit-test": "lint-staged",
    "pre-push-test": "jest --ci --bail --forceExit --silent --noStackTrace --reporters jest-dot-reporter",
  }
}

// .git root package.json
{
  "name": "MyProject",
  "version": "1.0.0",
  "config": {
    "ghooks": {
      "pre-commit": "cwd PATH/TO/SUB/DIRECTORY && npm run pre-commit-test",
      "pre-push": "cwd PATH/TO/SUB/DIRECTORY && npm run pre-push-test"
    }
  },
  "devDependencies": {
    "ghooks": "^2.0.3"
  }
}

@cesarp
Copy link

cesarp commented Apr 24, 2018

I am facing the same issue as @yiochen, postcommit fails with the same error, my project is inside a repo that contains backend, front end and test automation code so I can't put a package.json at the root.

/Project
  /FE
   /App -> package.json here

The log ends with

husky > npm run -s postcommit (node v8.2.1)
fatal: not a git repository: '.git'
husky > post-commit hook failed (add --no-verify to bypass)

@sudo-suhas
Copy link
Collaborator

Is there any reason to think that the failure in post commit hook is related to lint-staged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

9 participants