Skip to content

Commit

Permalink
v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshmikanth2001 committed Apr 9, 2024
1 parent b22935d commit 5e24ce1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 73 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to the "git-hooks" extension will be documented in this file

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [2.1.1] -- 2024-04-09
- not chaing `GitHooks.hooksDirectory` configuration on load to prevent it from git tracking
- workspace bug fix while switching between scm and activity bar view
- remove hook selection on workspace which has more than one folder
- bug fix in getting git config for hooks directory

## [2.0.1] -- 2023-12-13
- CacheContainer class added to cache git hooks directory reads
- Single Hook Directory read correction
Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "githooks",
"displayName": "GitHooks -- Simple VS Code UI for git hooks.",
"description": "Integrating Git Hooks to vscode UI; Can View, Edit and Run your local Git Hooks which can prevent your git repository from potential issues and enforce code quality.",
"version": "2.0.1",
"version": "2.1.1",
"license": "SEE LICENSE IN LICENSE",
"publisher": "lakshmikanthayyadevara",
"main": "./out/extension.js",
Expand Down Expand Up @@ -78,11 +78,6 @@
"command": "git-hooks.toggleView",
"when": "view == git_hooks_view||view == git_hooks_scm",
"group": "navigation"
},
{
"command": "git-hooks.configureHooksDirectory",
"when": "view == git_hooks_scm",
"group": "navigation"
}
],
"view/item/context": [
Expand Down Expand Up @@ -220,7 +215,7 @@
"type": "string",
"scope": "resource",
"default": "",
"description": "Directories to search for git hooks (git config core.hooksPath)"
"description": "Directories to search for git hooks (git config core.hooksPath only for single folder opened not for workspaces) "
},
"GitHooks.predefinedHooks": {
"type": "array",
Expand Down
50 changes: 31 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ function configurationChangeHandler(
} else {
vscode.window.showInformationMessage('GitHooks are now moved to Source Control view of vscode');
}
registerHookTreeDataProvider();
const hookDir = cacheInstance.get<Array<string>>('hooksDirectoryList');

if(hookDir && hookDir.length > 1){
registerMultiHookTreeDataProvider(hookDir);
}
else if (hookDir && hookDir.length === 1) {
registerHookTreeDataProvider(false, hookDir[0]);
}
else{
vscode.window.showErrorMessage('hooksDirectory is not valid for this workspace');
}
vscode.commands.executeCommand('setContext', 'GitHooks.viewContainerDisplay', viewContainerDisplay);
}
if (configChange.affectsConfiguration('GitHooks.hooksDirectory', workspaceFolder)) {
Expand Down Expand Up @@ -111,7 +121,8 @@ function configurationChangeHandler(

setHooksDir(hooksDir)
.then(() => {
registerHookTreeDataProvider();
cacheInstance.set('hooksDirectoryList', [hooksDir]);
reloadHooks();
vscode.window.showInformationMessage(`GitHooks directory is now set to ${hooksDir}`);
})
.catch((err) => {
Expand Down Expand Up @@ -159,18 +170,18 @@ async function executeShellCommandSync(command: string): Promise<string> {
* @param {boolean} globalFlag - A boolean indicating whether the hooks directory is global.
* @returns {Promise<string>} - A promise that resolves to the hooks directory.
**/
async function getHooksDir(globalFlag?: boolean): Promise<string> {
async function getHooksDir(globalFlag?: boolean, currentWorkspaceFolder?: string): Promise<string> {
// get all files in current workspace
// get coreHooksPath by executing this command git config --get core.hooksPath

// pipe error to null
// pipe error to null go to that dir and execute the git command
let hooksPath = await executeShellCommandSync(
`git config --get core.hooksPath ${globalFlag ? '--global' : ''} &2>/dev/null`,
`cd ${currentWorkspaceFolder} && git config --get core.hooksPath ${globalFlag ? '--global' : ''} &2>/dev/null`,
);

if (hooksPath && hooksPath !== '') {
// store this in vscode setContext
return hooksPath;
return hooksPath.replace(/\n$/, '');;
}

const workspaceFolders = vscode.workspace.workspaceFolders;
Expand Down Expand Up @@ -263,7 +274,9 @@ function selectHooksDir(): Promise<void> {
}
setHooksDir(uri[0].fsPath)
.then(() => {
cacheInstance.set('hooksDirectoryList', [uri[0].fsPath]);
vscode.window.showInformationMessage(`GitHooks directory is now set to ${uri[0].fsPath}`);
reloadHooks();
resolve();
})
.catch((err) => {
Expand Down Expand Up @@ -427,8 +440,10 @@ export async function activate(context: vscode.ExtensionContext) {

}
else{

const currentWorkspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;
// get local hooks path
getHooksDir()
getHooksDir(false, currentWorkspaceFolder)
.then((hooksDir) => {

cacheInstance.set('multipleWorkspace', false);
Expand All @@ -437,24 +452,21 @@ export async function activate(context: vscode.ExtensionContext) {

const currentConfiguration = vscode.workspace.getConfiguration('GitHooks', workspaceFolder);

let currentHooksDir = currentConfiguration?.['hooksDirectory'] ?? '';
let currentHooksDir: string| undefined = currentConfiguration?.inspect<string>('hooksDirectory')?.workspaceValue;

if (!path.isAbsolute(currentHooksDir)) {
currentHooksDir = path.resolve(workspaceFolder?.uri.fsPath ?? '', currentHooksDir);
if(!currentHooksDir){
currentHooksDir = path.join(workspaceFolder?.uri.fsPath ?? '', '.git', 'hooks');
}

// if hooks directory is not set in configuration
if (!currentConfiguration?.['hooksDirectory'] ?? false) {
currentConfiguration?.update('hooksDirectory', hooksDir, vscode.ConfigurationTarget.Workspace);
else if (!path.isAbsolute(currentHooksDir)) {
currentHooksDir = path.resolve(workspaceFolder?.uri.fsPath ?? '', currentHooksDir);
}

if (path.isAbsolute(currentHooksDir) && currentHooksDir !== hooksDir) {
else if (path.isAbsolute(currentHooksDir) && currentHooksDir !== hooksDir) {
// mismatch in hooks directory configuration

// update the configuration with git config core.hooksPath
logger.warn(`GitHooks: hooksDirectory configuration mismatch with git config core.hooksPath`);
vscode.window.showWarningMessage(
`GitHooks: hooksDirectory configuration is not matching with git config core.hooksPath. Updating the configuration with git config core.hooksPath value`,
`GitHooks: hooksDirectory configuration is not matching with git config core.hooksPath. Updating the configuration with (git config core.hooksPath <your_path>) or update hooksDirectory configuration in vscode settings`,
);

currentConfiguration?.update('hooksDirectory', hooksDir, vscode.ConfigurationTarget.Workspace);
Expand All @@ -464,9 +476,9 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.executeCommand('setContext', 'GitHooks.hooksDirectory', hooksDir);
vscode.commands.executeCommand('setContext', 'GitHooks.hooksDirectoryList', [hooksDir]);

cacheInstance.set('hooksDirectory', hooksDir);
cacheInstance.set('hooksDirectoryList', [hooksDir]);

registerHookTreeDataProvider();
registerHookTreeDataProvider(false, hooksDir);
intialHooksDirectorySet = false;
})
.catch((err) => {
Expand Down
14 changes: 10 additions & 4 deletions src/hook_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,20 @@ function reloadHooks() {

const workSpaceFolders = vscode.workspace.workspaceFolders;

if(workSpaceFolders?.length === 1){
const hookPaths = cacheInstance.get<Array<string>>('hooksDirectoryList');

if(!hookPaths){
vscode.window.showErrorMessage("hookPath is not cached and can not be accessed on reload action")
return ;
}

if (workSpaceFolders?.length === 1) {
// rebuild the TreeDataProvider
registerHookTreeDataProvider(true);
registerHookTreeDataProvider(true, hookPaths[0]);
return;
}

// multiple workspace support
registerMultiHookTreeDataProvider([]);
registerMultiHookTreeDataProvider(hookPaths);
}

export { openHook, runHook, toggleHook, reloadHooks, hookDescription, runCurrentHook };
64 changes: 21 additions & 43 deletions src/hooks_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,6 @@ export class Hook extends vscode.TreeItem {
contextValue?: string | undefined = 'hook';
}

function getMultiHookDirectories(): string[] {
const hookDirectories = vscode.workspace.workspaceFolders?.map((workspaceFolder) => {
let currentHooksDir = vscode.workspace.getConfiguration('GitHooks', workspaceFolder)?.['hooksDirectory'];

currentHooksDir = currentHooksDir.replace(/^~/, os.homedir());

if (!path.isAbsolute(currentHooksDir)) {
currentHooksDir = path.resolve(workspaceFolder?.uri.fsPath ?? '', currentHooksDir);
}
return currentHooksDir;
})??[];

if(hookDirectories.length === 0){
logger.error("No valid git hook directories found");
return [];
}
return hookDirectories;
}

export function validViewBadgeVersion(): boolean {
const initialValidVersion = '1.72.0'; // version where `viewBadge` feature was introduced
const baseRelease = initialValidVersion.split('.').map(Number);
Expand Down Expand Up @@ -89,11 +70,6 @@ export function registerMultiHookTreeDataProvider(hookDirectories: string[]) {
return;
}

// for reload action
if(hookDirectories.length === 0){
hookDirectories = getMultiHookDirectories();
}

const viewContainerDisplay = vscode.workspace.getConfiguration('GitHooks')?.['viewContainerDisplay'] ?? true;

let coreHooksProvider: MultiGitHooksProvider = new MultiGitHooksProvider(hookDirectories);
Expand All @@ -107,21 +83,23 @@ export function registerMultiHookTreeDataProvider(hookDirectories: string[]) {

scmHookTreeView.badge = undefined;
}
const coreHookTreeView = vscode.window.createTreeView('git_hooks_view', {
treeDataProvider: coreHooksProvider,
});
else{
const coreHookTreeView = vscode.window.createTreeView('git_hooks_view', {
treeDataProvider: coreHooksProvider,
});

if (validViewBadgeVersion()) {
const activeHookCount = coreHooksProvider.totalActiveHookCount;
const iconBadge: ViewBadge = {
value: activeHookCount,
tooltip: `${activeHookCount} ActiveHook${activeHookCount === 1 ? '' : 's'}`,
};
coreHookTreeView.badge = activeHookCount > 0 ? iconBadge : { value: 0, tooltip: '' };
if (validViewBadgeVersion()) {
const activeHookCount = coreHooksProvider.totalActiveHookCount;
const iconBadge: ViewBadge = {
value: activeHookCount,
tooltip: `${activeHookCount} ActiveHook${activeHookCount === 1 ? '' : 's'}`,
};
coreHookTreeView.badge = activeHookCount > 0 ? iconBadge : { value: 0, tooltip: '' };
}
}
}

export function registerHookTreeDataProvider(reloadFlag: boolean = false) {
export function registerHookTreeDataProvider(reloadFlag: boolean = false, hookDirectory: string) {
const workSpaceFolder = vscode.workspace.workspaceFolders;

// get scmProvider and coreProvider from cache
Expand Down Expand Up @@ -158,12 +136,13 @@ export function registerHookTreeDataProvider(reloadFlag: boolean = false) {
// if scm view
if (!viewContainerDisplay) {
// i think scm view should be created only once(or on reload)
let scmHookProvider = new GitHooksProvider(workingDir, true);
let scmHookProvider = new GitHooksProvider(workingDir, true, hookDirectory);
const gitHooksSCMView = vscode.window.createTreeView('git_hooks_scm', {
treeDataProvider: scmHookProvider,
});
gitHooksSCMView.title = `GitHooks (${scmHookProvider.activeHookCount})`;
gitHooksSCMView.description = getMultiHookDirectories()[0];

gitHooksSCMView.description = hookDirectory;
// clear the badge for scm
// scmHookProvider.badge = { value: 0, tooltip: '' };
cacheInstance.set('scmHookProvider', scmHookProvider);
Expand All @@ -189,7 +168,7 @@ export function registerHookTreeDataProvider(reloadFlag: boolean = false) {
return;
}

const coreHooksProvider = new GitHooksProvider(workingDir, false);
const coreHooksProvider = new GitHooksProvider(workingDir, false, hookDirectory);
const coreHookTreeView = vscode.window.createTreeView('git_hooks_view', {
treeDataProvider: coreHooksProvider,
});
Expand All @@ -212,10 +191,10 @@ export class GitHooksProvider implements vscode.TreeDataProvider<Hook> {
private predefinedHooksMap: Map<String, boolean>;

// get extension context
constructor(private workspaceRoot: string, private isFromScm: boolean) {
constructor(private workspaceRoot: string, private isFromScm: boolean, hookDirectory: string) {
vscode.workspace.onDidChangeWorkspaceFolders((e) => this.onActivateWorkspaceChanged(e));
this.onActivateWorkspaceChanged(undefined);
this.gitHooksDir = getMultiHookDirectories()[0];
this.gitHooksDir = hookDirectory;

const predefinedHooks: string[] = vscode.workspace.getConfiguration('GitHooks')?.['predefinedHooks'] ?? [];

Expand Down Expand Up @@ -271,9 +250,8 @@ export class GitHooksProvider implements vscode.TreeDataProvider<Hook> {
// for the root element with label githooks on the top
let hook = new Hook('Hooks', '', vscode.TreeItemCollapsibleState.Expanded, "");
hook.contextValue = 'root';
const currentHookDirectories = getMultiHookDirectories();
hook.description = currentHookDirectories[0];
hook.tooltip = 'Hooks Directory \n' + currentHookDirectories[0];
hook.description = this.gitHooksDir;
hook.tooltip = 'Hooks Directory \n' + this.gitHooksDir;

if (this.activeHookCount > 0) {
hook.iconPath = new vscode.ThemeIcon('testing-passed-icon', new vscode.ThemeColor('#RRGGBBAA'));
Expand Down

0 comments on commit 5e24ce1

Please sign in to comment.