From 364d580ff896691ee70d3735f495c720651a9f41 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 20 Jun 2024 11:06:53 +0100 Subject: [PATCH] feat(git): Add authentication to clone, fetch, and pull. --- packages/git/src/subcommands/clone.js | 12 ++++++++++++ packages/git/src/subcommands/fetch.js | 15 ++++++++++++++- packages/git/src/subcommands/pull.js | 12 ++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/git/src/subcommands/clone.js b/packages/git/src/subcommands/clone.js index b932f7fd3..7cfa3f146 100644 --- a/packages/git/src/subcommands/clone.js +++ b/packages/git/src/subcommands/clone.js @@ -20,6 +20,7 @@ import git from 'isomorphic-git'; import http from 'isomorphic-git/http/web'; import { SHOW_USAGE } from '../help.js'; import path from 'path-browserify'; +import { authentication_options, Authenticator } from '../auth.js'; export default { name: 'clone', @@ -46,6 +47,7 @@ export default { type: 'boolean', default: false, }, + ...authentication_options, }, }, execute: async (ctx) => { @@ -74,6 +76,15 @@ export default { throw SHOW_USAGE; } + if (!options.username !== !options.password) { + stderr('Please specify both --username and --password, or neither'); + return 1; + } + const authenticator = new Authenticator({ + username: options.username, + password: options.password, + }); + let repo_path; if (directory) { repo_path = path.resolve(env.PWD, directory); @@ -113,6 +124,7 @@ export default { singleBranch: options['single-branch'], noTags: options['no-tags'], onMessage: (message) => { stdout(message); }, + ...authenticator.get_auth_callbacks(stderr), }); } } diff --git a/packages/git/src/subcommands/fetch.js b/packages/git/src/subcommands/fetch.js index d8e586b9b..74418f02c 100644 --- a/packages/git/src/subcommands/fetch.js +++ b/packages/git/src/subcommands/fetch.js @@ -20,6 +20,7 @@ import git from 'isomorphic-git'; import http from 'isomorphic-git/http/web'; import { determine_fetch_remote, find_repo_root } from '../git-helpers.js'; import { SHOW_USAGE } from '../help.js'; +import { authentication_options, Authenticator } from '../auth.js'; export default { name: 'fetch', @@ -35,7 +36,8 @@ export default { description: 'Fetch all remotes.', type: 'boolean', default: false, - } + }, + ...authentication_options, }, }, execute: async (ctx) => { @@ -54,6 +56,15 @@ export default { gitdir, }); + if (!options.username !== !options.password) { + stderr('Please specify both --username and --password, or neither'); + return 1; + } + const authenticator = new Authenticator({ + username: options.username, + password: options.password, + }); + if (options.all) { for (const { remote, url } of remotes) { stdout(`Fetching ${remote}\nFrom ${url}`); @@ -66,6 +77,7 @@ export default { gitdir, remote, onMessage: (message) => { stdout(message); }, + ...authenticator.get_auth_callbacks(stderr), }); } return; @@ -83,6 +95,7 @@ export default { gitdir, ...remote_data, onMessage: (message) => { stdout(message); }, + ...authenticator.get_auth_callbacks(stderr), }); } } diff --git a/packages/git/src/subcommands/pull.js b/packages/git/src/subcommands/pull.js index 06d024c47..3fc71af75 100644 --- a/packages/git/src/subcommands/pull.js +++ b/packages/git/src/subcommands/pull.js @@ -20,6 +20,7 @@ import git from 'isomorphic-git'; import http from 'isomorphic-git/http/web'; import { determine_fetch_remote, find_repo_root } from '../git-helpers.js'; import { SHOW_USAGE } from '../help.js'; +import { authentication_options, Authenticator } from '../auth.js'; export default { name: 'pull', @@ -43,6 +44,7 @@ export default { description: 'Only update history if a fast-forward is possible.', type: 'boolean', }, + ...authentication_options, }, }, execute: async (ctx) => { @@ -74,6 +76,15 @@ export default { throw SHOW_USAGE; } + if (!options.username !== !options.password) { + stderr('Please specify both --username and --password, or neither'); + return 1; + } + const authenticator = new Authenticator({ + username: options.username, + password: options.password, + }); + const remote_data = determine_fetch_remote(remote, remotes); await git.pull({ fs, @@ -87,6 +98,7 @@ export default { fastForward: options['ff'], fastForwardOnly: options['ff-only'], onMessage: (message) => { stdout(message); }, + ...authenticator.get_auth_callbacks(stderr), }); } };