diff --git a/src/commandsAndMenu.tsx b/src/commandsAndMenu.tsx index 8c588be17..63362154e 100644 --- a/src/commandsAndMenu.tsx +++ b/src/commandsAndMenu.tsx @@ -1367,26 +1367,35 @@ export async function showGitOperationDialog( operation: Operation, trans: TranslationBundle, args?: T, - authentication?: Git.IAuth, - retry = false + authentication?: Git.IAuthWithCacheCredentials, + retry = false, + cache = false ): Promise { try { + const auth = authentication ? { + username: authentication.username, + password: authentication.password + } as Git.IAuth : undefined; let result: Git.IResultWithMessage; // the Git action switch (operation) { case Operation.Clone: // eslint-disable-next-line no-case-declarations const { path, url } = args as any as IGitCloneArgs; - result = await model.clone(path, url, authentication); + result = await model.clone(path, + url, + auth, + authentication ? authentication.cacheCredentials : false + ); break; case Operation.Pull: result = await model.pull(authentication); break; case Operation.Push: - result = await model.push(authentication); + result = await model.push(authentication, false, authentication.cacheCredentials); break; case Operation.ForcePush: - result = await model.push(authentication, true); + result = await model.push(authentication, true, authentication.cacheCredentials); break; default: result = { code: -1, message: 'Unknown git command' }; @@ -1407,7 +1416,10 @@ export async function showGitOperationDialog( trans, trans.__('Enter credentials for remote repository'), retry ? trans.__('Incorrect username or password.') : '' - ) + )//, + //buttons: [ + // Dialog.cacheCredentials({label: trans.__('Cache Credentials?')}) + //] }); if (credentials.button.accept) { diff --git a/src/model.ts b/src/model.ts index 80543b844..38afa6e47 100644 --- a/src/model.ts +++ b/src/model.ts @@ -577,7 +577,8 @@ export class GitExtension implements IGitExtension { async clone( path: string, url: string, - auth?: Git.IAuth + auth?: Git.IAuth, + cacheCredentials = false ): Promise { return await this._taskHandler.execute( 'git:clone', @@ -587,7 +588,8 @@ export class GitExtension implements IGitExtension { 'POST', { clone_url: url, - auth: auth as any + auth: auth as any, + cache_credentials: cacheCredentials } ); } @@ -822,7 +824,7 @@ export class GitExtension implements IGitExtension { * @throws {Git.GitResponseError} If the server response is not ok * @throws {ServerConnection.NetworkError} If the request cannot be made */ - async pull(auth?: Git.IAuth): Promise { + async pull(auth?: Git.IAuth, cache_credentials: true): Promise { const path = await this._getPathRepository(); const data = this._taskHandler.execute( 'git:pull', @@ -855,7 +857,7 @@ export class GitExtension implements IGitExtension { * @throws {Git.GitResponseError} If the server response is not ok * @throws {ServerConnection.NetworkError} If the request cannot be made */ - async push(auth?: Git.IAuth, force = false): Promise { + async push(auth?: Git.IAuth, cache_credentials: true, force = false, cacheCredentials = false): Promise { const path = await this._getPathRepository(); const data = this._taskHandler.execute( 'git:push', @@ -865,7 +867,8 @@ export class GitExtension implements IGitExtension { 'POST', { auth: auth as any, - force: force + force: force, + cache_credentials: cacheCredentials } ); } @@ -1494,7 +1497,22 @@ export class GitExtension implements IGitExtension { private _fetchRemotes = async (): Promise => { try { const path = await this._getPathRepository(); - await requestAPI(URLExt.join(path, 'remote', 'fetch'), 'POST'); + + const credentials = await showDialog({ + title: trans.__('Git credentials required'), + body: new GitCredentialsForm( + trans, + trans.__('Enter credentials for remote repository'), + retry ? trans.__('Incorrect username or password.') : '' + )//, + //buttons: [ + // Dialog.cacheCredentials({label: trans.__('Cache Credentials?')}) + //] + }); + + await requestAPI(URLExt.join(path, 'remote', 'fetch'), 'POST', { + auth?: Git.IAuth, cache_credentials: true + }); } catch (error) { console.error('Failed to fetch remotes', error); } diff --git a/src/tokens.ts b/src/tokens.ts index f682749ac..80a124cc3 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -926,6 +926,13 @@ export namespace Git { password: string; } + /** + * Interface for the Git Auth request with credentials caching option + */ + export interface IAuthWithCacheCredentials extends IAuth { + cacheCredentials: boolean; + } + /** * Structure for the request to the Git Clone API. */ diff --git a/src/widgets/CredentialsBox.tsx b/src/widgets/CredentialsBox.tsx index e20d4b337..021e60431 100755 --- a/src/widgets/CredentialsBox.tsx +++ b/src/widgets/CredentialsBox.tsx @@ -23,10 +23,14 @@ export class GitCredentialsForm private createBody(textContent: string, warningContent: string): HTMLElement { const node = document.createElement('div'); const label = document.createElement('label'); + const checkBoxLabel = document.createElement('label'); + this._checkboxCacheCredentials = document.createElement('input'); this._user = document.createElement('input'); this._password = document.createElement('input'); + //this._cacheCredentials = document.createElement('input'); this._password.type = 'password'; + const text = document.createElement('span'); const warning = document.createElement('div'); @@ -35,13 +39,20 @@ export class GitCredentialsForm text.textContent = textContent; warning.textContent = warningContent; this._user.placeholder = this._trans.__('username'); + this._checkboxCacheCredentials.type = 'checkbox'; + this._checkboxCacheCredentials.textContent = this._trans.__("Save my login temporarily"); + //this._cacheCredentials.placeholder = this._trans.__('Cache credentials?'); this._password.placeholder = this._trans.__( 'password / personal access token' + ); label.appendChild(text); label.appendChild(this._user); label.appendChild(this._password); + checkBoxLabel.appendChild(this._checkboxCacheCredentials); + node.appendChild(checkBoxLabel); + //label.appendChild(this._cacheCredentials); node.appendChild(label); node.appendChild(warning); return node; @@ -50,13 +61,17 @@ export class GitCredentialsForm /** * Returns the input value. */ - getValue(): Git.IAuth { + getValue(): Git.IAuthWithCacheCredentials { return { username: this._user.value, - password: this._password.value + password: this._password.value, + cacheCredentials: this._checkboxCacheCredentials.checked }; + // return this._cacheCredentials; } protected _trans: TranslationBundle; private _user: HTMLInputElement; private _password: HTMLInputElement; + private _checkboxCacheCredentials: HTMLInputElement; + //private _cacheCredentials: HTMLInputElement; }