Skip to content

WIP: Support for git repository #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
"default": "",
"description": "%ext.config.gist%"
},
"sync.repoUrl": {
"type": "string",
"default": null,
"description": "%ext.config.repoUrl%"
},
"sync.autoDownload": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -171,6 +176,7 @@
"fs-extra": "^7.0.0",
"https-proxy-agent": "^2.2.1",
"lockfile": "^1.0.4",
"simple-git": "^1.107.0",
"temp": "^0.8.3"
}
}
7 changes: 7 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ext.config.title": "Code Settings Sync Configuration Settings",
"ext.config.gist": "GitHub GIST ID for Settings Sync.",
"ext.config.repoUrl": "Provide remote url to push changes.",
"ext.config.lastUpload": "Settings Sync last upload date. Set it as empty if you want to manually hit download.",
"ext.config.lastDownload": "Settings Sync last download date. Set it as empty if you want to manually hit download.",
"ext.config.autoDownload": "Set it true to Auto Download the settings on code start. [Code Restart Required]",
Expand Down Expand Up @@ -50,6 +51,8 @@
"cmd.otherOptions.toggleAutoDownload.on": "Sync : Auto Download turned ON upon VSCode Startup.",
"cmd.otherOptions.toggleAutoDownload.off": "Sync : Auto Download turned OFF upon VSCode Startup.",
"cmd.otherOptions.toggleSummaryPage": "Sync : Toggle Show Summary Page On Upload / Download",
"cmd.otherOptions.toggleSyncMode": "Sync : Toggle Sync mode - gist / repo",
"cmd.otherOptions.toggleSyncMode.changed": "Sync : Sync mode changed to {0}",
"cmd.otherOptions.preserve": "Sync : Preserve Setting To Stop Override After Download",
"cmd.otherOptions.preserve.placeholder": "Enter any Key from settings.json to preserve.",
"cmd.otherOptions.preserve.prompt": "Example : Write 'http.proxy' => store this computer proxy and overwrite it , if set empty it will remove proxy.",
Expand Down Expand Up @@ -86,6 +89,10 @@
"common.error.invalidGistId": "Sync : Invalid Gist Id Entered. Verify your gist : https://gist.github.com/<your_userName>/<gist_id>.",
"common.error.tokenNotSave": "Sync : Token Not Saved.",
"common.error.gistNotSave": "Sync : Gist Not Saved.",
"common.error.noGit": "Sync : Git is not installed",
"common.error.noGitUser": "Sync : Git user or email is not configured",
"common.error.noGitRepoUrl": "Sync : Git repository is not set",
"common.error.fetchFirst": "Sync : Remote repository has different settings. Please download first and then retry",
"common.action.openExtPage": "Open Extension Page",
"common.action.openExtTutorial": "Open Tutorial",
"common.action.releaseNotes": "Release Notes",
Expand Down
46 changes: 33 additions & 13 deletions src/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,35 @@ export default class Commons {
} else if (error.code === 4) {
message = localize("common.error.canNotSave");
} else if (error.message) {
try {
message = JSON.parse(error.message).message;
if (message.toLowerCase() === "bad credentials") {
msgBox = true;
message = localize("common.error.invalidToken");
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/settings/tokens'));
}
if (message.toLowerCase() === "not found") {
msgBox = true;
message = localize("common.error.invalidGistId");
if (error.message.indexOf("spawn git ENOENT") !== -1) {
msgBox = true;
message = localize("common.error.noGit");
} else if (error.message.indexOf("Please tell me who you are") !== -1) {
msgBox = true;
message = localize("common.error.noGitUser");
} else if (
error.message.indexOf("No configured push destination") !== -1
) {
msgBox = true;
message = localize("common.error.noGitRepoUrl");
} else if (error.message.indexOf("fetch first") !== -1) {
msgBox = true;
message = localize("common.error.fetchFirst");
} else {
try {
message = JSON.parse(error.message).message;
if (message.toLowerCase() === "bad credentials") {
msgBox = true;
message = localize("common.error.invalidToken");
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/settings/tokens'));
}
if (message.toLowerCase() === "not found") {
msgBox = true;
message = localize("common.error.invalidGistId");
}
} catch (error) {
// message = error.message;
}
} catch (error) {
// message = error.message;
}
}
}
Expand Down Expand Up @@ -256,6 +272,10 @@ export default class Commons {
askToken = !cusSettings.downloadPublicGist;
}

if (cusSettings.syncMode === "repo" && extSettings.repoUrl !== "") {
askToken = false;
}

if (askToken) {
if (cusSettings.openTokenLink) {
vscode.commands.executeCommand(
Expand All @@ -273,7 +293,7 @@ export default class Commons {
}
}

if (extSettings.gist === "") {
if (extSettings.gist === "" && cusSettings.syncMode === "gist") {
if (askGist) {
const gistTemp: string = await this.GetGistAndSave(extSettings);
if (!gistTemp) {
Expand Down
13 changes: 13 additions & 0 deletions src/service/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export class File {
public filePath: string,
public gistName: string
) {}

public Write(): Promise<boolean> {
return FileService.WriteFile(this.filePath, this.content)
}

public ResolvePath(parentDir: string): void {
this.filePath = parentDir + path.sep + this.fileName
}
}
export class FileService {
public static CUSTOMIZED_SYNC_PREFIX = "|customized_sync|";
Expand Down Expand Up @@ -72,6 +80,11 @@ export class FileService {
return file;
}

public static async GetFileFromPath(filePath: string): Promise<File> {
const fileName = path.basename(filePath);
return await FileService.GetFile(filePath, fileName);
}

public static async WriteFile(
filePath: string,
data: string
Expand Down
7 changes: 7 additions & 0 deletions src/service/githubService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import * as GitHubApi from "@octokit/rest";
import { ReposCreateParams } from "@octokit/rest";
import * as HttpsProxyAgent from "https-proxy-agent";
import * as vscode from "vscode";
import { File } from "./fileService";
Expand Down Expand Up @@ -154,4 +155,10 @@ export class GitHubService {
await this.github.gists.edit(gistObject);
return true;
}

public async MakeRepository(
options: GitHubApi.ReposCreateParams
): Promise<GitHubApi.Response<GitHubApi.ReposCreateResponse>> {
return await this.github.repos.create(options);
}
}
63 changes: 63 additions & 0 deletions src/service/localGitService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"use strict";

import * as git from "simple-git/promise";
import { File } from "./fileService";

export class LocalGitService {
private git: git.SimpleGit = null;

constructor(userPath: string) {
this.git = git(userPath);
}

public async Init(remoteUrl: string): Promise<void> {
await this._setupGit();
await this._setupRemote(remoteUrl);
}

public async Add(files: File[]): Promise<void> {
await this.git.add(files.map(file => file.filePath));
}

public async Commit(message: string): Promise<void> {
await this.git.commit(message);
}

public async Push(): Promise<void> {
await this.git.push("origin", "master", {
"--set-upstream": null,
"--force": null
});
}

public async Pull(): Promise<void> {
await this.git.checkout(["-B", "master", "origin/master"]);
}

public async Files(): Promise<string[]> {
return await this.git
.raw(["ls-tree", "--full-tree", "--name-only", "-r", "HEAD"])
.then(result => result.split(/\r\n|\r|\n/));
}

private async _setupGit(): Promise<void> {
if (!(await this.git.checkIsRepo())) {
await this.git.init();
}
}

private async _setupRemote(remoteUrl: string): Promise<void> {
if (remoteUrl) {
const remote = await this.git
.getRemotes(true)
.then(remotes => remotes.filter(v => v.name === "origin").shift());
if (remote) {
if (remote.refs.push === remoteUrl) {
return;
}
await this.git.removeRemote("origin");
}
await this.git.addRemote("origin", remoteUrl);
}
}
}
3 changes: 3 additions & 0 deletions src/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Environment } from "./environmentPath";

export class ExtensionConfig {
public gist: string = null;
public repoUrl: string = null;
public quietSync: boolean = false;
public removeExtensions: boolean = true;
public syncExtensions: boolean = true;
Expand Down Expand Up @@ -54,4 +55,6 @@ export class CustomSettings {
public askGistName: boolean = false;
public customFiles: { [key: string]: string } = {};
public hostName: string = null;
public syncMode: "gist" | "repo" = "gist";
public repoPush: boolean = false;
}
Loading