Skip to content

Commit 0a4a201

Browse files
authored
Fix missing error messages in invalid or expired git credentials (#335)
1 parent 5ea980e commit 0a4a201

File tree

7 files changed

+51
-26
lines changed

7 files changed

+51
-26
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"theme": "dark"
99
},
1010
"publisher": "Katsute",
11-
"version": "2.0.1",
11+
"version": "2.0.2",
1212
"private": true,
1313
"engines": {
1414
"vscode": "^1.110.0"

src/command/auth.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import * as fs from "fs";
2222
import * as os from "os";
2323

2424
import { isNull, isValidJson } from "../lib/is";
25+
import * as config from "../config";
2526
import * as logger from "../logger";
2627
import * as files from "../lib/files";
2728
import { Crypt } from "../lib/encrypt";
2829
import * as extension from "../extension";
2930
import { Distribution } from "../distribution";
3031
import { CommandQuickPickItem } from "../lib/quickpick";
32+
import simpleGit from "simple-git";
3133

3234
//
3335

@@ -52,6 +54,11 @@ const crypt: Crypt = new Crypt(os.hostname());
5254

5355
//
5456

57+
const parseRepo: (repo: string, cred: credentials) => string = (repo: string, cred: credentials) => {
58+
const part: string[] = repo.split("://");
59+
return `${part[0]}://${cred.login}:${cred.auth}@${part.slice(1).join("://")}`;
60+
}
61+
5562
export const mask: (s: string, c: credentials) => string = (s: string, c: credentials) => {
5663
return s.replace(new RegExp(c.auth, "gm"), "***");
5764
}
@@ -78,9 +85,27 @@ export const authenticate: () => void = () => {
7885
if(value.trim().length === 0)
7986
return "Token can not be blank";
8087
}
81-
}).then((password?: string) => {
88+
}).then(async (password?: string) => {
8289
if(!password) return;
8390

91+
const repo: string = config.get("repository");
92+
93+
if(repo){
94+
const cred: credentials = { login: username, auth: password };
95+
const remote: string = parseRepo(repo, cred);
96+
97+
const err = await simpleGit().listRemote([remote])
98+
.then(() => {
99+
logger.info(`Credentials are valid for ${repo}`);
100+
})
101+
.catch(e => {
102+
logger.error(`Failed to verify credentials for ${repo}:\n ${mask(String(e?.message ?? e), cred)}`, true);
103+
return e;
104+
});
105+
106+
if(err) return;
107+
}
108+
84109
const dist: Distribution = extension.distribution();
85110

86111
logger.info(`Updated authentication: ${username}`);
@@ -91,7 +116,8 @@ export const authenticate: () => void = () => {
91116
"login": "${username}",
92117
"auth": "${crypt.encrypt(password)}"
93118
}`,
94-
"utf-8");
119+
"utf-8"
120+
);
95121
});
96122
});
97123
}

src/command/local.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ export const item: CommandQuickPickItem = {
3131
}
3232

3333
export const command: vscode.Disposable = vscode.commands.registerCommand("settings-repository.overwriteLocal", () => {
34-
config.get("repository") && pull(config.get("repository"));
34+
config.get("repository") && pull(config.get("repository"), config.get("branch"));
3535
});

src/command/remote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ export const item: CommandQuickPickItem = {
3131
}
3232

3333
export const command: vscode.Disposable = vscode.commands.registerCommand("settings-repository.overwriteRemote", () => {
34-
config.get("repository") && push(config.get("repository"));
34+
config.get("repository") && push(config.get("repository"), config.get("branch"));
3535
});

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ export const activate: (context: vscode.ExtensionContext) => void = (context: vs
6868
logger.debug(`branch: ${config.get("branch")}`);
6969
logger.debug(`autoSync: ${config.get("autoSync")}`);
7070
logger.debug(`includeHostnameInCommitMessage: ${config.get("includeHostnameInCommitMessage")}`);
71-
logger.debug(`authenticated: ${!!auth.authorization()}`);
71+
logger.debug(`has credentials: ${!!auth.authorization()}`);
7272

7373
if(config.get("autoSync") === true && config.get("autoSyncMode") !== "Export Only")
74-
config.get("repository") && pull(config.get("repository"), true);
74+
config.get("repository") && pull(config.get("repository"), config.get("branch"), true);
7575
}
7676

7777
// must be async, otherwise vscode closes without waiting
7878
export const deactivate: () => Promise<void> = async () => {
7979
if(config.get("autoSync") === true && config.get("autoSyncMode") !== "Import Only")
80-
config.get("repository") && await push(config.get("repository"), true);
80+
config.get("repository") && await push(config.get("repository"), config.get("branch"), true);
8181
}
8282

8383
export const notify: () => void = () => {

src/sync/git.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ const parseRepo: (repo: string, cred: auth.credentials) => string = (repo: strin
4747
return `${part[0]}://${cred.login}:${cred.auth}@${part.slice(1).join("://")}`;
4848
}
4949

50-
export const pull: (repo: string, skipNotify?: boolean) => void = async (repo: string, skipNotify: boolean = false) => {
50+
export const pull: (repo: string, branch?: string, skipNotify?: boolean) => Promise<void> = async (repo: string, branch: string = "main", skipNotify: boolean = false) => {
5151
if(isNull(repo)) return;
5252

5353
const dist: Distribution = extension.distribution();
5454
const cred: auth.credentials | undefined = auth.authorization();
5555

56-
if(!cred) return skipNotify || auth.authenticate();
56+
if(!cred) {
57+
skipNotify || auth.authenticate();
58+
return;
59+
}
5760

5861
// init directory
5962

@@ -65,13 +68,11 @@ export const pull: (repo: string, skipNotify?: boolean) => void = async (repo: s
6568

6669
const remote: string = parseRepo(repo, cred);
6770

68-
const branch: string = config.get("branch") ?? "main";
69-
7071
// callback
7172

7273
const gitback: (err: GitError | null) => void = (err: GitError | null) => {
7374
if(err){
74-
logger.error(`Failed to pull from ${config.get("repository")}:\n ${auth.mask(err.message, cred)}`, true);
75+
logger.error(`Failed to pull from ${repo}@${branch}:\n ${auth.mask(err.message, cred)}`, true);
7576
cleanup(temp);
7677
}
7778
};
@@ -80,7 +81,7 @@ export const pull: (repo: string, skipNotify?: boolean) => void = async (repo: s
8081

8182
statusbar.setActive(true);
8283

83-
logger.info(`Preparing to import settings from ${config.get("repository")}@${branch}`);
84+
logger.info(`Preparing to import settings from ${repo}@${branch}`);
8485
logger.debug(`Git clone ${auth.mask(remote, cred)}`);
8586

8687
// forced delay so git repo can get up-to-date after a fast reload/restart
@@ -148,21 +149,21 @@ export const pull: (repo: string, skipNotify?: boolean) => void = async (repo: s
148149
logger.warn("Snippets not found");
149150
}
150151

151-
logger.info(`Imported settings from ${config.get("repository")}@${branch}`, true);
152+
logger.info(`Imported settings from ${repo}@${branch}`, true);
152153

153154
cleanup(temp);
154155

155156
skipNotify || extension.notify();
156157
}
157158
});
158159
}catch(error: any){
159-
logger.error(`Push failed: ${auth.mask(error, cred)}`, true);
160+
logger.error(`Pull failed: ${auth.mask(String(error?.message ?? error), cred)}`, true);
160161
}finally{
161162
cleanup(temp);
162163
}
163164
}
164165

165-
export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = async (repo: string, ignoreBadAuth: boolean = false) => {
166+
export const push: (repo: string, branch?: string, ignoreBadAuth?: boolean) => Promise<void> = async (repo: string, branch: string = "main", ignoreBadAuth: boolean = false) => {
166167
if(isNull(repo)) return;
167168

168169
const dist: Distribution = extension.distribution();
@@ -180,13 +181,11 @@ export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = as
180181

181182
const remote: string = parseRepo(repo, cred);
182183

183-
const branch: string = config.get("branch") ?? "main";
184-
185184
// callback
186185

187186
const gitback: (err: GitError | null) => void = (err: GitError | null) => {
188187
if(err){
189-
logger.error(`Failed to push to ${config.get("repository")}:\n ${auth.mask(err.message, cred)}`, true);
188+
logger.error(`Failed to push to ${repo}@${branch}:\n ${auth.mask(err.message, cred)}`, true);
190189
cleanup(temp);
191190
}
192191
};
@@ -195,7 +194,7 @@ export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = as
195194

196195
statusbar.setActive(true);
197196

198-
logger.info(`Preparing to export settings to ${config.get("repository")}@${branch}`);
197+
logger.info(`Preparing to export settings to ${repo}@${branch}`);
199198
logger.debug(`Git clone ${auth.mask(remote, cred)}`);
200199
logger.debug(`includeHostnameInCommit: ${config.get("includeHostnameInCommitMessage")}`);
201200

@@ -257,7 +256,7 @@ export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = as
257256
}
258257
}catch(error: any){
259258
if(error){
260-
logger.error(`Push failed: ${auth.mask(error, cred)}`, true);
259+
logger.error(`Push failed: ${auth.mask(String(error), cred)}`, true);
261260
cleanup(temp);
262261
}
263262
}
@@ -271,12 +270,12 @@ export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = as
271270
.push(["-u", "origin", "HEAD"], (err: GitError | null) => {
272271
gitback(err);
273272
if(!err){
274-
logger.info(`Pushed settings to ${config.get("repository")}@${branch}`, true);
273+
logger.info(`Pushed settings to ${repo}@${branch}`, true);
275274
cleanup(temp);
276275
}
277276
});
278277
}catch(error: any){
279-
logger.error(`Push failed: ${auth.mask(error, cred)}`, true);
278+
logger.error(`Push failed: ${auth.mask(String(error?.message ?? error), cred)}`, true);
280279
}finally{
281280
cleanup(temp);
282281
}

0 commit comments

Comments
 (0)