Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"theme": "dark"
},
"publisher": "Katsute",
"version": "2.0.1",
"version": "2.0.2",
"private": true,
"engines": {
"vscode": "^1.110.0"
Expand Down
30 changes: 28 additions & 2 deletions src/command/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import * as fs from "fs";
import * as os from "os";

import { isNull, isValidJson } from "../lib/is";
import * as config from "../config";
import * as logger from "../logger";
import * as files from "../lib/files";
import { Crypt } from "../lib/encrypt";
import * as extension from "../extension";
import { Distribution } from "../distribution";
import { CommandQuickPickItem } from "../lib/quickpick";
import simpleGit from "simple-git";

//

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

//

const parseRepo: (repo: string, cred: credentials) => string = (repo: string, cred: credentials) => {
const part: string[] = repo.split("://");
return `${part[0]}://${cred.login}:${cred.auth}@${part.slice(1).join("://")}`;
}

export const mask: (s: string, c: credentials) => string = (s: string, c: credentials) => {
return s.replace(new RegExp(c.auth, "gm"), "***");
}
Expand All @@ -78,9 +85,27 @@ export const authenticate: () => void = () => {
if(value.trim().length === 0)
return "Token can not be blank";
}
}).then((password?: string) => {
}).then(async (password?: string) => {
if(!password) return;

const repo: string = config.get("repository");

if(repo){
const cred: credentials = { login: username, auth: password };
const remote: string = parseRepo(repo, cred);

const err = await simpleGit().listRemote([remote])
.then(() => {
logger.info(`Credentials are valid for ${repo}`);
})
.catch(e => {
logger.error(`Failed to verify credentials for ${repo}:\n ${mask(String(e?.message ?? e), cred)}`, true);
return e;
});

if(err) return;
}

const dist: Distribution = extension.distribution();

logger.info(`Updated authentication: ${username}`);
Expand All @@ -91,7 +116,8 @@ export const authenticate: () => void = () => {
"login": "${username}",
"auth": "${crypt.encrypt(password)}"
}`,
"utf-8");
"utf-8"
);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export const item: CommandQuickPickItem = {
}

export const command: vscode.Disposable = vscode.commands.registerCommand("settings-repository.overwriteLocal", () => {
config.get("repository") && pull(config.get("repository"));
config.get("repository") && pull(config.get("repository"), config.get("branch"));
});
2 changes: 1 addition & 1 deletion src/command/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export const item: CommandQuickPickItem = {
}

export const command: vscode.Disposable = vscode.commands.registerCommand("settings-repository.overwriteRemote", () => {
config.get("repository") && push(config.get("repository"));
config.get("repository") && push(config.get("repository"), config.get("branch"));
});
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ export const activate: (context: vscode.ExtensionContext) => void = (context: vs
logger.debug(`branch: ${config.get("branch")}`);
logger.debug(`autoSync: ${config.get("autoSync")}`);
logger.debug(`includeHostnameInCommitMessage: ${config.get("includeHostnameInCommitMessage")}`);
logger.debug(`authenticated: ${!!auth.authorization()}`);
logger.debug(`has credentials: ${!!auth.authorization()}`);

if(config.get("autoSync") === true && config.get("autoSyncMode") !== "Export Only")
config.get("repository") && pull(config.get("repository"), true);
config.get("repository") && pull(config.get("repository"), config.get("branch"), true);
}

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

export const notify: () => void = () => {
Expand Down
31 changes: 15 additions & 16 deletions src/sync/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ const parseRepo: (repo: string, cred: auth.credentials) => string = (repo: strin
return `${part[0]}://${cred.login}:${cred.auth}@${part.slice(1).join("://")}`;
}

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

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

if(!cred) return skipNotify || auth.authenticate();
if(!cred) {
skipNotify || auth.authenticate();
return;
}

// init directory

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

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

const branch: string = config.get("branch") ?? "main";

// callback

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

statusbar.setActive(true);

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

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

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

cleanup(temp);

skipNotify || extension.notify();
}
});
}catch(error: any){
logger.error(`Push failed: ${auth.mask(error, cred)}`, true);
logger.error(`Pull failed: ${auth.mask(String(error?.message ?? error), cred)}`, true);
}finally{
cleanup(temp);
}
}

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

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

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

const branch: string = config.get("branch") ?? "main";

// callback

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

statusbar.setActive(true);

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

Expand Down Expand Up @@ -257,7 +256,7 @@ export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = as
}
}catch(error: any){
if(error){
logger.error(`Push failed: ${auth.mask(error, cred)}`, true);
logger.error(`Push failed: ${auth.mask(String(error), cred)}`, true);
cleanup(temp);
}
}
Expand All @@ -271,12 +270,12 @@ export const push: (repo: string, ignoreBadAuth?: boolean) => Promise<void> = as
.push(["-u", "origin", "HEAD"], (err: GitError | null) => {
gitback(err);
if(!err){
logger.info(`Pushed settings to ${config.get("repository")}@${branch}`, true);
logger.info(`Pushed settings to ${repo}@${branch}`, true);
cleanup(temp);
}
});
}catch(error: any){
logger.error(`Push failed: ${auth.mask(error, cred)}`, true);
logger.error(`Push failed: ${auth.mask(String(error?.message ?? error), cred)}`, true);
}finally{
cleanup(temp);
}
Expand Down
Loading