Skip to content

Commit f82bdb9

Browse files
committed
update to allow globally installing via npm
1 parent d6ba266 commit f82bdb9

File tree

4 files changed

+134
-13
lines changed

4 files changed

+134
-13
lines changed

README.md

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,66 @@ Because this helper is designed to allow sharing credentials on the same machine
1010

1111
## Installation and Usage
1212

13-
This helper is written in Typescript and compiles down to two Javascript scripts, one for the server and one for the client.
13+
This helper is written in TypeScript and can be installed globally via npm or pnpm.
1414

15-
### Download
15+
### Installation Options
1616

17-
Download the latest release from this repo. The release consists of a filed named `git-credential-forwarder.zip` which contains two Javascript scripts: `gcf-server.js` and `gcf-client.js`. These can be placed wherever you want, but these instructions assume they are placed in the home directories of the host and container.
17+
#### Global Installation (Recommended)
18+
19+
Install the package globally using npm or pnpm:
20+
21+
```
22+
npm install -g git-credential-forwarder
23+
# or
24+
pnpm add -g git-credential-forwarder
25+
```
26+
27+
This will make the commands `gcf-server` and `gcf-client` available globally.
28+
29+
#### Manual Download
30+
31+
Alternatively, you can download the latest release from this repo. The release consists of a file named `git-credential-forwarder.zip` which contains two JavaScript scripts: `gcf-server.js` and `gcf-client.js`. These can be placed wherever you want.
32+
33+
After downloading, make the scripts executable:
34+
35+
```
36+
chmod +x gcf-server.js gcf-client.js
37+
```
1838

1939
### On the host
2040

21-
Run `node ~/gcf-server.js`. This will launch the server and it will listen for TCP connections on localhost at a random port which will be displayed in the console. You will need to keep this console/terminal open.
41+
If installed globally:
42+
```
43+
gcf-server
44+
```
45+
46+
If using manual download:
47+
```
48+
./gcf-server.js
49+
```
50+
51+
This will launch the server and it will listen for TCP connections on localhost at a random port which will be displayed in the console. You will need to keep this console/terminal open.
2252

2353
Notes:
2454

2555
- You can tell it to use a specific port by setting the environmental variable `GIT_CREDENTIAL_FORWARDER_PORT`
2656

2757
### In the container
2858

29-
Run `export GIT_CREDENTIAL_FORWARDER_SERVER="host.Docker.internal:PORT` where PORT is replaced with the port displayed when you ran the server.
59+
Run `export GIT_CREDENTIAL_FORWARDER_SERVER="host.docker.internal:PORT"` where PORT is replaced with the port displayed when you ran the server.
60+
61+
Edit your git configuration file to call the client as a git credential helper:
3062

31-
Edit your git configuration file to call the client you just complied as a git credential helper, as follows:
63+
If installed globally:
64+
```
65+
[credential]
66+
helper = "!f() { gcf-client $*; }; f"
67+
```
3268

69+
If using manual download:
3370
```
3471
[credential]
35-
helper = "!f() { node ~/gcf-client.js $*; }; f"
72+
helper = "!f() { /path/to/gcf-client.js $*; }; f"
3673
```
3774

3875
Run git normally and all requests for credentials should be passed through to the host which will handle appropriately on the host side.
@@ -46,14 +83,33 @@ Notes:
4683

4784
Here's a strategy to make this fairly easy to use with a Docker container built with a Dockerfile.
4885

86+
#### Option 1: Using npm or pnpm (Recommended)
87+
88+
On the host, set a specific port that you will listen on by configuring the env variable `GIT_CREDENTIAL_FORWARDER_PORT`.
89+
90+
Add these lines in the Dockerfile:
91+
92+
```
93+
# Install Node.js and npm/pnpm first if needed
94+
RUN npm install -g git-credential-forwarder
95+
# or
96+
RUN pnpm add -g git-credential-forwarder
97+
98+
RUN git config --global credential.helper '!f(){ gcf-client $*; }; f'
99+
ENV GIT_CREDENTIAL_FORWARDER_SERVER host.docker.internal:[PORT]
100+
```
101+
102+
#### Option 2: Using direct download
103+
49104
On the host, set a specific port that you will listen on by configuring the env variable `GIT_CREDENTIAL_FORWARDER_PORT`.
50105

51-
Add these lines in the Dockerfile
106+
Add these lines in the Dockerfile:
52107

53108
```
54109
RUN curl -LO https://github.com/sam-mfb/git-credential-forwarder/releases/download/v[VERSION]/git-credential-forwarder.zip
55-
RUN unzip git-credential-forwarder.zip
56-
RUN git config --global credential.helper '!f(){ node ~/gcf-client.js $*; }; f'
110+
RUN unzip git-credential-forwarder.zip -d /usr/local/bin
111+
RUN chmod +x /usr/local/bin/gcf-*.js
112+
RUN git config --global credential.helper '!f(){ /usr/local/bin/gcf-client.js $*; }; f'
57113
ENV GIT_CREDENTIAL_FORWARDER_SERVER host.docker.internal:[PORT]
58114
```
59115

add-shebang.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
// Add shebang and make files executable for global npm installation
6+
const shebang = '#!/usr/bin/env node\n';
7+
const files = ['gcf-server.js', 'gcf-client.js'];
8+
const distDir = path.join(__dirname, 'dist');
9+
10+
for (const file of files) {
11+
const filePath = path.join(distDir, file);
12+
13+
if (fs.existsSync(filePath)) {
14+
const content = fs.readFileSync(filePath, 'utf8');
15+
16+
// Only add shebang if it doesn't already exist
17+
if (!content.startsWith('#!')) {
18+
fs.writeFileSync(filePath, shebang + content);
19+
console.log(`Added shebang to ${file}`);
20+
21+
// Make file executable
22+
try {
23+
fs.chmodSync(filePath, '755');
24+
console.log(`Made ${file} executable`);
25+
} catch (error) {
26+
console.error(`Failed to make ${file} executable:`, error.message);
27+
}
28+
} else {
29+
console.log(`Shebang already exists in ${file}`);
30+
}
31+
} else {
32+
console.error(`File ${file} does not exist in dist directory`);
33+
}
34+
}

package.json

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,49 @@
33
"version": "1.2.0",
44
"description": "utilities for forwarding git credential helper commands to another git installation (e.g. container to host)",
55
"main": "dist/index.js",
6+
"bin": {
7+
"git-credential-forwarder-server": "./dist/gcf-server.js",
8+
"git-credential-forwarder-client": "./dist/gcf-client.js",
9+
"gcf-server": "./dist/gcf-server.js",
10+
"gcf-client": "./dist/gcf-client.js"
11+
},
612
"scripts": {
7-
"build": "rimraf ./dist && webpack",
13+
"build": "rimraf ./dist && webpack && node ./add-shebang.js",
814
"build-mock-git": "rimraf ./src/__tests__/dist && webpack -c ./webpack.mock-git.js",
915
"zip": "zip -j dist/git-credential-forwarder.zip dist/gcf-*",
1016
"test": "jest --watch",
1117
"e2e-test": "jest -c ./jest.e2e.config.js",
1218
"lint": "eslint .",
1319
"fix-formatting": "prettier --write --config ./prettier.config.js ./src/",
14-
"check-formatting": "prettier --check --config ./prettier.config.js ./src/"
20+
"check-formatting": "prettier --check --config ./prettier.config.js ./src/",
21+
"prepublishOnly": "npm run build && npm run lint && npm test -- --watchAll=false"
1522
},
1623
"author": "Sam Davidoff",
1724
"license": "MIT",
25+
"keywords": [
26+
"git",
27+
"credential",
28+
"helper",
29+
"docker",
30+
"container",
31+
"forwarder"
32+
],
33+
"repository": {
34+
"type": "git",
35+
"url": "git+https://github.com/sam-mfb/git-credential-forwarder.git"
36+
},
37+
"bugs": {
38+
"url": "https://github.com/sam-mfb/git-credential-forwarder/issues"
39+
},
40+
"homepage": "https://github.com/sam-mfb/git-credential-forwarder#readme",
41+
"engines": {
42+
"node": ">=18.0.0"
43+
},
44+
"files": [
45+
"dist",
46+
"README.md",
47+
"LICENSE.md"
48+
],
1849
"devDependencies": {
1950
"@eslint/js": "^9.9.0",
2051
"@types/eslint__js": "^8.42.3",

src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ if (serverType === "tcp" && portEnv) {
110110
`Edit your git configuration file inside your docker container to call the git-credential-forwarder client script, for example:\n`
111111
)
112112
configOutput(` [credential]`)
113-
configOutput(` helper = "!f() { node ~/gcf-client.js $*; }; f"\n`)
113+
configOutput(` helper = "!f() { gcf-client $*; }; f"\n`)
114114

115115
try {
116116
await credentialReceiver()

0 commit comments

Comments
 (0)