Skip to content

Commit 364d580

Browse files
AtkinsSJKernelDeimos
authored andcommitted
feat(git): Add authentication to clone, fetch, and pull.
1 parent 8c70229 commit 364d580

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

packages/git/src/subcommands/clone.js

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import git from 'isomorphic-git';
2020
import http from 'isomorphic-git/http/web';
2121
import { SHOW_USAGE } from '../help.js';
2222
import path from 'path-browserify';
23+
import { authentication_options, Authenticator } from '../auth.js';
2324

2425
export default {
2526
name: 'clone',
@@ -46,6 +47,7 @@ export default {
4647
type: 'boolean',
4748
default: false,
4849
},
50+
...authentication_options,
4951
},
5052
},
5153
execute: async (ctx) => {
@@ -74,6 +76,15 @@ export default {
7476
throw SHOW_USAGE;
7577
}
7678

79+
if (!options.username !== !options.password) {
80+
stderr('Please specify both --username and --password, or neither');
81+
return 1;
82+
}
83+
const authenticator = new Authenticator({
84+
username: options.username,
85+
password: options.password,
86+
});
87+
7788
let repo_path;
7889
if (directory) {
7990
repo_path = path.resolve(env.PWD, directory);
@@ -113,6 +124,7 @@ export default {
113124
singleBranch: options['single-branch'],
114125
noTags: options['no-tags'],
115126
onMessage: (message) => { stdout(message); },
127+
...authenticator.get_auth_callbacks(stderr),
116128
});
117129
}
118130
}

packages/git/src/subcommands/fetch.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import git from 'isomorphic-git';
2020
import http from 'isomorphic-git/http/web';
2121
import { determine_fetch_remote, find_repo_root } from '../git-helpers.js';
2222
import { SHOW_USAGE } from '../help.js';
23+
import { authentication_options, Authenticator } from '../auth.js';
2324

2425
export default {
2526
name: 'fetch',
@@ -35,7 +36,8 @@ export default {
3536
description: 'Fetch all remotes.',
3637
type: 'boolean',
3738
default: false,
38-
}
39+
},
40+
...authentication_options,
3941
},
4042
},
4143
execute: async (ctx) => {
@@ -54,6 +56,15 @@ export default {
5456
gitdir,
5557
});
5658

59+
if (!options.username !== !options.password) {
60+
stderr('Please specify both --username and --password, or neither');
61+
return 1;
62+
}
63+
const authenticator = new Authenticator({
64+
username: options.username,
65+
password: options.password,
66+
});
67+
5768
if (options.all) {
5869
for (const { remote, url } of remotes) {
5970
stdout(`Fetching ${remote}\nFrom ${url}`);
@@ -66,6 +77,7 @@ export default {
6677
gitdir,
6778
remote,
6879
onMessage: (message) => { stdout(message); },
80+
...authenticator.get_auth_callbacks(stderr),
6981
});
7082
}
7183
return;
@@ -83,6 +95,7 @@ export default {
8395
gitdir,
8496
...remote_data,
8597
onMessage: (message) => { stdout(message); },
98+
...authenticator.get_auth_callbacks(stderr),
8699
});
87100
}
88101
}

packages/git/src/subcommands/pull.js

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import git from 'isomorphic-git';
2020
import http from 'isomorphic-git/http/web';
2121
import { determine_fetch_remote, find_repo_root } from '../git-helpers.js';
2222
import { SHOW_USAGE } from '../help.js';
23+
import { authentication_options, Authenticator } from '../auth.js';
2324

2425
export default {
2526
name: 'pull',
@@ -43,6 +44,7 @@ export default {
4344
description: 'Only update history if a fast-forward is possible.',
4445
type: 'boolean',
4546
},
47+
...authentication_options,
4648
},
4749
},
4850
execute: async (ctx) => {
@@ -74,6 +76,15 @@ export default {
7476
throw SHOW_USAGE;
7577
}
7678

79+
if (!options.username !== !options.password) {
80+
stderr('Please specify both --username and --password, or neither');
81+
return 1;
82+
}
83+
const authenticator = new Authenticator({
84+
username: options.username,
85+
password: options.password,
86+
});
87+
7788
const remote_data = determine_fetch_remote(remote, remotes);
7889
await git.pull({
7990
fs,
@@ -87,6 +98,7 @@ export default {
8798
fastForward: options['ff'],
8899
fastForwardOnly: options['ff-only'],
89100
onMessage: (message) => { stdout(message); },
101+
...authenticator.get_auth_callbacks(stderr),
90102
});
91103
}
92104
};

0 commit comments

Comments
 (0)