Skip to content

Commit

Permalink
fix: default git repository value if exist (#315)
Browse files Browse the repository at this point in the history
Fix for correctly inspect git config value and use remote repository url
as repository value in default pacakage json value

Fixes npm/cli#7940

Testing 
```sh
~/workarea/test-repo $ lnpm init -y
Wrote to /Users/milaninfy/workarea/test-repo/package.json:

{
  "name": "test-repo",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/milaninfy/test-repo.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/milaninfy/test-repo/issues"
  },
  "homepage": "https://github.com/milaninfy/test-repo#readme",
  "description": ""
}
```
  • Loading branch information
milaninfy authored Dec 2, 2024
1 parent eab6037 commit c96ad4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/default-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,17 @@ if (!package.scripts) {

if (!package.repository) {
exports.repository = async () => {
const gconf = await fs.readFile('.git/config', 'utf8').catch(() => '')
const gitConfigPath = path.resolve(dirname, '.git', 'config')
const gconf = await fs.readFile(gitConfigPath, 'utf8').catch(() => '')
const lines = gconf.split(/\r?\n/)

let url
const i = lines.indexOf('[remote "origin"]')

if (i !== -1) {
url = gconf[i + 1]
url = lines[i + 1]
if (!url.match(/^\s*url =/)) {
url = gconf[i + 2]
url = lines[i + 2]
}
if (!url.match(/^\s*url =/)) {
url = null
Expand Down
20 changes: 20 additions & 0 deletions test/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ t.test('license', async (t) => {
}
t.has(data, wanted)
})

t.test('repository from git config', async (t) => {
const testdir = {
'.git': {
config: `
[remote "origin"]
url = https://github.com/npm/cli.git`,
} }

const { data } = await setup(t, __filename, {
config: { yes: 'yes' },
testdir,
})

const wanted = {
type: 'git',
url: 'git+https://github.com/npm/cli.git',
}
t.has(data.repository, wanted)
})

0 comments on commit c96ad4a

Please sign in to comment.