- check if in git repository before running commands

- can "disable" extention if not in git repository
This commit is contained in:
nyncral 2024-07-19 15:18:40 +02:00
parent 20f35ab6e5
commit 519dbb96cf
3 changed files with 74 additions and 63 deletions

View file

@ -1,16 +1,16 @@
# Change Log # Change Log
All notable changes to the "dwm-git-simpleuse" extension will be documented in this file. ## [Unreleased] <sub><sup><sub>_(not in order)_</sub></sup></sub>
## [Unreleased] <span style="font-size:0.5em;">_(not in order)_</span>
- Task API - Task API
- No git msgs as errors - No git msgs as errors
- check if in git repository before running commands
- Progress bar for git commands - Progress bar for git commands
- Upload only active file - Upload only active file
- Make it possible to receive a webhook to send info to user - Make it possible to receive a webhook to send info to user
- check if in git repository before running commands
- can "disable" extention if not in git repository
## [0.1.3] - 2024-07-18 ## [0.1.3] - 2024-07-18
- Fixed "commit message auto increment" - Fixed "commit message auto increment"

View file

@ -1,25 +1,34 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode"); const vscode = require("vscode");
const util = require("node:util");
const chp = require("child_process"); const chp = require("child_process");
const execP = util.promisify(chp.exec);
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/** /**
* @param {vscode.ExtensionContext} context * @param {vscode.ExtensionContext} context
*/ */
function activate(context) { async function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
let lastGitCommitMsg = ""; let lastGitCommitMsg = "";
if (await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled")) {
try { try {
process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath); process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath);
} catch (err) { } catch (err) {
vscode.window.showErrorMessage(err); vscode.window.showErrorMessage(err);
} }
chp.execFile("git", ["pull"], (err, stdout, stderr) => {
const { stdout } = await execP(`git rev-parse --is-inside-work-tree || echo false`, {
cwd: vscode.workspace.workspaceFolders[0].uri.fsPath,
});
if (stdout.trim() !== "true")
vscode.window
.showErrorMessage("Not a git repository", "Disable extension for current workspace")
.then((e) => {
if (e == undefined) return;
vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).update("isEnabled", false);
});
else
chp.execFile("git", ["pull", "--ff"], (err, stdout, stderr) => {
if (err) { if (err) {
vscode.window.showErrorMessage("GITPULL : " + err.message); vscode.window.showErrorMessage("GITPULL : " + err.message);
return; return;
@ -30,11 +39,19 @@ function activate(context) {
} }
vscode.window.showInformationMessage("GITPULL : " + stdout); vscode.window.showInformationMessage("GITPULL : " + stdout);
}); });
}
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand("dwm-git-simpleuse.fullPush", async function () { let disposable = vscode.commands.registerCommand("dwm-git-simpleuse.fullPush", async function () {
if (!(await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled"))) {
vscode.window
.showErrorMessage("Extension disabled", "Enable extension for current workspace")
.then((e) => {
if (e == undefined) return;
vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).update("isEnabled", true);
});
return;
}
const gitCommitMsg = await vscode.window.showInputBox({ const gitCommitMsg = await vscode.window.showInputBox({
placeHolder: "commit message", placeHolder: "commit message",
prompt: "choose your commit message", prompt: "choose your commit message",
@ -49,7 +66,10 @@ function activate(context) {
lastGitCommitMsg = gitCommitMsg; lastGitCommitMsg = gitCommitMsg;
chp.exec( chp.exec(
`git add . && git commit -am "${gitCommitMsg.replace(/"/g, '\\"')}" && git pull --ff && git push`, `git add . && git commit -am "${gitCommitMsg.replace(
/"/g,
'\\"'
)}" && git pull --ff && git push`,
{ cwd: vscode.workspace.workspaceFolders[0].uri.fsPath }, { cwd: vscode.workspace.workspaceFolders[0].uri.fsPath },
(err, stdout, stderr) => { (err, stdout, stderr) => {
if (err) { if (err) {
@ -63,41 +83,20 @@ function activate(context) {
vscode.window.showInformationMessage(stdout); vscode.window.showInformationMessage(stdout);
} }
); );
/* try {
process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath);
} catch (err) {
vscode.window.showErrorMessage(err);
} */
/* chp.execFile("git", ["add", "*"], (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage("GITADD : " + err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage("GITADD : " + stderr);
return;
}
vscode.window.showInformationMessage("GITADD : " + stdout);
});
chp.execFile(
"git",
["commit", "-m test7", "-a"],
(err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage("GITCOM : " + err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage("GITCOM : " + stderr);
return;
}
vscode.window.showInformationMessage("GITCOM : " + stdout);
}
); */
}); });
context.subscriptions.push(disposable); context.subscriptions.push(disposable);
disposable = vscode.commands.registerCommand("dwm-git-simpleuse.initRepo", async function () { disposable = vscode.commands.registerCommand("dwm-git-simpleuse.initRepo", async function () {
if (!(await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled"))) {
vscode.window
.showErrorMessage("Extension disabled", "Enable extension for current workspace")
.then((e) => {
if (e == undefined) return;
vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).update("isEnabled", true);
});
return;
}
const gitLink = await vscode.window.showInputBox({ const gitLink = await vscode.window.showInputBox({
placeHolder: "git repo link", placeHolder: "git repo link",
prompt: "Initialize a git repository", prompt: "Initialize a git repository",

View file

@ -32,7 +32,19 @@
"key": "ctrl+alt+s", "key": "ctrl+alt+s",
"mac": "ctrl+alt+s" "mac": "ctrl+alt+s"
} }
] ],
"configuration": {
"type": "object",
"title": "DWM Git Simple Use Configuration",
"properties": {
"dwm-git-simpleuse.isEnabled": {
"type": "boolean",
"default": true,
"description": "Enable DWM Git Simple Use extension",
"scope": "resource"
}
}
}
}, },
"scripts": { "scripts": {
"lint": "eslint .", "lint": "eslint .",