Skip to content

Commit 68b7c96

Browse files
committed
Never include .git folders in package root
Git folders are in the default ignore set. However, occasionally git can create a file named 'readme', or another of npm's 'always include' filenames. This roots the always-include filename set to the root with a /, and also avoids even walking the root's .git folder. (Git folders can still be included explicitly deeper in the package, if they are included in a package.json files list, or un-ignored in a .gitignore or .npmignore.)
1 parent 1a4c8bf commit 68b7c96

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: index.js

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ const npmWalker = Class => class Walker extends Class {
9090
}
9191
}
9292

93+
onReaddir (entries) {
94+
if (!this.parent)
95+
entries = entries.filter(e => e !== '.git')
96+
return super.onReaddir(entries)
97+
}
98+
9399
filterEntry (entry, partial) {
94100
// get the partial path from the root of the walk
95101
const p = this.path.substr(this.root.length + 1)
@@ -147,6 +153,14 @@ const npmWalker = Class => class Walker extends Class {
147153

148154
// if there's a bin, browser or main, make sure we don't ignore it
149155
// also, don't ignore the package.json itself!
156+
//
157+
// Weird side-effect of this: a readme (etc) file will be included
158+
// if it exists anywhere within a folder with a package.json file.
159+
// The original intent was only to include these files in the root,
160+
// but now users in the wild are dependent on that behavior for
161+
// localized documentation and other use cases. Adding a `/` to
162+
// these rules, while tempting and arguably more "correct", is a
163+
// breaking change.
150164
const rules = [
151165
pkg.browser ? '!' + pkg.browser : '',
152166
pkg.main ? '!' + pkg.main : '',

Diff for: test/ignores.js

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ t.test('setup', t => {
121121
path.join(gitDir, 'gitstub'),
122122
"won't fool git, also won't be included"
123123
)
124+
const gitDeep = path.join(pkg, '.git/logs/refs/remotes/name')
125+
mkdirp.sync(gitDeep)
126+
fs.writeFileSync(
127+
path.join(gitDeep, 'readme'),
128+
'please do not include git dirs (or even walk to them)'
129+
)
124130

125131
const historyDir = path.join(pkg, 'node_modules/history')
126132
mkdirp.sync(historyDir)

0 commit comments

Comments
 (0)