acp-push 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/bin/acp.js +137 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,6 +43,25 @@ acp -u -b staging # git push upstream staging
|
|
|
43
43
|
acp -m "Fix login bug" # custom message
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
## Multiple GitHub accounts
|
|
47
|
+
|
|
48
|
+
If you have the [GitHub CLI](https://cli.github.com) (`gh`) installed, `acp` picks the right account for each repo automatically:
|
|
49
|
+
|
|
50
|
+
| Remote | Account used |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `https://github.com/SENODROOM/acp-push` | `SENODROOM` |
|
|
53
|
+
| `https://github.com/Gobibahu/some-repo` | `GobiBahu` (names are case-insensitive) |
|
|
54
|
+
| `https://github.com/some-org/project` (no matching account) | the account you used last |
|
|
55
|
+
|
|
56
|
+
Log in to each account once:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
gh auth login # log in as the first account
|
|
60
|
+
gh auth login # run again for the second account
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`acp` switches gh's active account to whichever one it used, so "last used" is always the most recent one. The token is only used for that one push and doesn't replace your saved git login. SSH remotes and non-GitHub remotes use your normal git login.
|
|
64
|
+
|
|
46
65
|
## Notes
|
|
47
66
|
|
|
48
67
|
- If there is nothing new to commit, `acp` skips the commit and still pushes, so any commits you made earlier get sent.
|
package/bin/acp.js
CHANGED
|
@@ -26,12 +26,17 @@ Options:
|
|
|
26
26
|
-h, --help Show this help
|
|
27
27
|
-v, --version Show the version
|
|
28
28
|
|
|
29
|
+
GitHub accounts:
|
|
30
|
+
If the GitHub CLI (gh) is installed, acp picks the gh account that matches
|
|
31
|
+
the repo owner (github.com/<owner>/...). If none matches, it uses the
|
|
32
|
+
account you used last. Log in to each account once with: gh auth login
|
|
33
|
+
|
|
29
34
|
Examples:
|
|
30
|
-
acp ${dim('#
|
|
31
|
-
acp -u ${dim('#
|
|
32
|
-
acp -b dev ${dim('#
|
|
33
|
-
acp -u --branch staging ${dim('#
|
|
34
|
-
acp -m "Fix login bug" ${dim('# custom message,
|
|
35
|
+
acp ${dim('# push to origin/main')}
|
|
36
|
+
acp -u ${dim('# push to upstream/main')}
|
|
37
|
+
acp -b dev ${dim('# push to origin/dev')}
|
|
38
|
+
acp -u --branch staging ${dim('# push to upstream/staging')}
|
|
39
|
+
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
35
40
|
`;
|
|
36
41
|
|
|
37
42
|
function fail(message) {
|
|
@@ -95,14 +100,19 @@ function parseArgs(argv) {
|
|
|
95
100
|
return options;
|
|
96
101
|
}
|
|
97
102
|
|
|
98
|
-
// ----------
|
|
103
|
+
// ---------- process helpers ----------
|
|
99
104
|
// Arguments are passed as an array (no shell), so commit messages with
|
|
100
105
|
// quotes, spaces or special characters are always safe.
|
|
101
|
-
function
|
|
102
|
-
|
|
103
|
-
stdio: quiet ? 'pipe' : 'inherit',
|
|
106
|
+
function run(cmd, args, { quiet = false, env, stdio } = {}) {
|
|
107
|
+
return spawnSync(cmd, args, {
|
|
108
|
+
stdio: stdio || (quiet ? 'pipe' : 'inherit'),
|
|
104
109
|
encoding: 'utf8',
|
|
110
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
105
111
|
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function git(args, options) {
|
|
115
|
+
const result = run('git', args, options);
|
|
106
116
|
if (result.error) {
|
|
107
117
|
if (result.error.code === 'ENOENT') fail('git is not installed or not on your PATH.');
|
|
108
118
|
fail(result.error.message);
|
|
@@ -110,41 +120,152 @@ function git(args, quiet = false) {
|
|
|
110
120
|
return result;
|
|
111
121
|
}
|
|
112
122
|
|
|
113
|
-
function step(args) {
|
|
123
|
+
function step(args, { extraArgs = [], env, hint } = {}) {
|
|
114
124
|
const shown = args.map((a) => (/[\s"']/.test(a) ? JSON.stringify(a) : a)).join(' ');
|
|
115
125
|
console.log(cyan(`› git ${shown}`));
|
|
116
|
-
|
|
117
|
-
|
|
126
|
+
// When a hint is set, capture git's error output so we can tell whether
|
|
127
|
+
// the push failed because of permissions (403, denied, auth failed).
|
|
128
|
+
const result = git([...extraArgs, ...args], {
|
|
129
|
+
env,
|
|
130
|
+
stdio: hint ? ['inherit', 'inherit', 'pipe'] : undefined,
|
|
131
|
+
});
|
|
132
|
+
if (hint && result.stderr) process.stderr.write(result.stderr);
|
|
133
|
+
if (result.status !== 0) {
|
|
134
|
+
const authError = /\b403\b|permission|denied|authentication failed|could not read username/i;
|
|
135
|
+
if (hint && authError.test(result.stderr || '')) console.error(yellow(hint));
|
|
136
|
+
fail(`"git ${args[0]}" failed (exit code ${result.status}).`);
|
|
137
|
+
}
|
|
118
138
|
}
|
|
119
139
|
|
|
120
140
|
function randomMessage() {
|
|
121
141
|
return messages[Math.floor(Math.random() * messages.length)];
|
|
122
142
|
}
|
|
123
143
|
|
|
144
|
+
// ---------- GitHub account picking (via gh CLI) ----------
|
|
145
|
+
function githubOwner(url) {
|
|
146
|
+
const match = url.match(/^https?:\/\/(?:[^@/]+@)?github\.com\/([^/]+)\//i);
|
|
147
|
+
return match ? match[1] : null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Returns { accounts: [...logins], active: login } or null.
|
|
151
|
+
function ghAccounts() {
|
|
152
|
+
const json = run('gh', ['auth', 'status', '--hostname', 'github.com', '--json', 'hosts'], { quiet: true });
|
|
153
|
+
if (json.error) return null; // gh is not installed
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const list = JSON.parse(json.stdout).hosts['github.com'] || [];
|
|
157
|
+
if (list.length) {
|
|
158
|
+
const active = list.find((a) => a.active) || list[0];
|
|
159
|
+
return { accounts: list.map((a) => a.login), active: active.login };
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
} catch (_) {
|
|
163
|
+
// Older gh without --json: read the plain-text status instead.
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const plain = run('gh', ['auth', 'status', '--hostname', 'github.com'], { quiet: true });
|
|
167
|
+
const text = `${plain.stdout || ''}\n${plain.stderr || ''}`;
|
|
168
|
+
const found = [...text.matchAll(/github\.com (?:account|as) ([A-Za-z0-9-]+)/g)];
|
|
169
|
+
if (!found.length) return null;
|
|
170
|
+
|
|
171
|
+
const accounts = found.map((m) => m[1]);
|
|
172
|
+
let active = accounts[0];
|
|
173
|
+
found.forEach((m, i) => {
|
|
174
|
+
const block = text.slice(m.index, found[i + 1] ? found[i + 1].index : undefined);
|
|
175
|
+
if (/Active account: true/.test(block)) active = m[1];
|
|
176
|
+
});
|
|
177
|
+
return { accounts, active };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function ghToken(login, isActive) {
|
|
181
|
+
let result = run('gh', ['auth', 'token', '--hostname', 'github.com', '--user', login], { quiet: true });
|
|
182
|
+
if (result.status !== 0 && isActive) {
|
|
183
|
+
result = run('gh', ['auth', 'token', '--hostname', 'github.com'], { quiet: true });
|
|
184
|
+
}
|
|
185
|
+
return result.status === 0 ? result.stdout.trim() : null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Works out which gh account to push with and returns the extra git
|
|
189
|
+
// arguments + env needed to use it. Returns null to use normal git login.
|
|
190
|
+
function githubAuth(remoteUrl) {
|
|
191
|
+
if (/^(ssh:\/\/)?git@github\.com[:/]/i.test(remoteUrl)) {
|
|
192
|
+
console.log(dim('• SSH remote, using your SSH key.'));
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const owner = githubOwner(remoteUrl);
|
|
197
|
+
if (!owner) return null; // not a GitHub https remote
|
|
198
|
+
|
|
199
|
+
const gh = ghAccounts();
|
|
200
|
+
if (!gh) {
|
|
201
|
+
console.log(dim('• GitHub CLI not found or not logged in, using your normal git login.'));
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// GitHub usernames are case-insensitive (Gobibahu === GobiBahu).
|
|
206
|
+
const match = gh.accounts.find((a) => a.toLowerCase() === owner.toLowerCase());
|
|
207
|
+
const account = match || gh.active;
|
|
208
|
+
|
|
209
|
+
if (match) {
|
|
210
|
+
console.log(cyan(`• Using GitHub account "${account}" (owner of this repo)`));
|
|
211
|
+
} else {
|
|
212
|
+
console.log(cyan(`• No gh account for "${owner}", using last used account "${account}"`));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Make it gh's active account, so it becomes the "last used" one.
|
|
216
|
+
if (account !== gh.active) {
|
|
217
|
+
const switched = run('gh', ['auth', 'switch', '--hostname', 'github.com', '--user', account], { quiet: true });
|
|
218
|
+
if (switched.status !== 0) console.log(yellow(`⚠ Could not switch gh to "${account}", continuing anyway.`));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const token = ghToken(account, account === gh.active);
|
|
222
|
+
if (!token) {
|
|
223
|
+
console.log(yellow(`⚠ Could not get a token for "${account}", using your normal git login.`));
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// A one-off credential helper just for this push: it clears any saved
|
|
228
|
+
// helpers (like Windows Credential Manager) and hands git the gh token.
|
|
229
|
+
// The token travels in an env var, never on the command line.
|
|
230
|
+
const helper =
|
|
231
|
+
'!f() { if [ "$1" = get ]; then echo "username=$ACP_GH_USER"; echo "password=$ACP_GH_TOKEN"; fi; }; f';
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
extraArgs: ['-c', 'credential.helper=', '-c', `credential.helper=${helper}`],
|
|
235
|
+
env: { ACP_GH_USER: account, ACP_GH_TOKEN: token },
|
|
236
|
+
hint:
|
|
237
|
+
`⚠ GitHub account "${account}" may not have push access to this repo.\n` +
|
|
238
|
+
` Log in to an account that does with: gh auth login`,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
124
242
|
// ---------- main ----------
|
|
125
243
|
function main() {
|
|
126
244
|
const { remote, branch, message } = parseArgs(process.argv.slice(2));
|
|
127
245
|
|
|
128
|
-
if (git(['rev-parse', '--is-inside-work-tree'], true).status !== 0) {
|
|
246
|
+
if (git(['rev-parse', '--is-inside-work-tree'], { quiet: true }).status !== 0) {
|
|
129
247
|
fail('Not inside a git repository.');
|
|
130
248
|
}
|
|
131
249
|
|
|
132
|
-
|
|
250
|
+
const remoteUrl = git(['remote', 'get-url', remote], { quiet: true });
|
|
251
|
+
if (remoteUrl.status !== 0) {
|
|
133
252
|
fail(`Remote "${remote}" does not exist. Add it with: git remote add ${remote} <url>`);
|
|
134
253
|
}
|
|
135
254
|
|
|
136
255
|
step(['add', '.']);
|
|
137
256
|
|
|
138
257
|
// "git diff --cached --quiet" exits 0 when nothing is staged.
|
|
139
|
-
if (git(['diff', '--cached', '--quiet'], true).status === 0) {
|
|
258
|
+
if (git(['diff', '--cached', '--quiet'], { quiet: true }).status === 0) {
|
|
140
259
|
console.log(yellow('• Nothing new to commit, pushing existing commits.'));
|
|
141
260
|
} else {
|
|
142
261
|
step(['commit', '-m', message || randomMessage()]);
|
|
143
262
|
}
|
|
144
263
|
|
|
264
|
+
const auth = githubAuth(remoteUrl.stdout.trim()) || {};
|
|
265
|
+
|
|
145
266
|
// "HEAD:<branch>" pushes whatever branch you're on (main, master, anything)
|
|
146
267
|
// to <branch> on the remote, so the default always lands on "main".
|
|
147
|
-
step(['push', remote, `HEAD:${branch}`]);
|
|
268
|
+
step(['push', remote, `HEAD:${branch}`], auth);
|
|
148
269
|
console.log(green(`✔ Pushed to ${remote}/${branch}`));
|
|
149
270
|
}
|
|
150
271
|
|