Skip to content
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

Base reference for diff #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
# README

![screenshot](media/desc.png)

## Features

- Display `git diff` result in editor status bar realtime
- Warn if git diff line count is too big

## Setting
## Settings

```json
{
// Max counts for 'changed files','insertions','deletions',and 'insertions deletions sum'.Set `null` to ignore specific one.
"gitDiffWarning.maxCountEachTypeAndSum": [
null,
200,
200,
200
]
// Max counts for 'changed files','insertions','deletions',and 'insertions deletions sum'.Set `null` to ignore specific one.
"gitDiffWarning.maxCountEachTypeAndSum": [null, 200, 200, 200],

// Define which branch, commit, tag or symbolic reference must be used as base for diff comparison. One can set 'origin/main', for instance.
"gitDiffWarning.baseReference": "HEAD"
}
```

### gitDiffWarning.baseReference

By default, the extension will compare against `HEAD` in local repository in order to count changes. That means changes are counted only since the last commit has been done in local folder.

However, one can use setting `gitDiffWarning.baseReference` to change that behaviour, for instance to "origin/main" or "origin/master", which will compare with the main branch in GitHub remote. That is particularly useful to forsee the size of the pull request that's going to be created out of this change, because sometimes we commit multiple times into a feature branch before creating a pull request for it.
127 changes: 66 additions & 61 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@
{
"name": "git-diff-warning",
"displayName": "Git Diff Warning",
"description": "Warn if git diff line count is too big",
"publisher": "nanlei2000",
"version": "1.0.2",
"engines": {
"vscode": "^1.39.0"
},
"categories": [
"Other"
],
"activationEvents": [
"workspaceContains:.git"
],
"main": "./out/extension.js",
"contributes": {
"configuration": [
{
"title": "Git Diff Warning Configuration",
"properties": {
"gitDiffWarning.maxCountEachTypeAndSum": {
"type": "array",
"default": [
null,
200,
200,
200
],
"description": "Max counts for 'changed files','insertions','deletions',and 'insertions deletions sum'.Set `null` to ignore specific one."
}
}
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.6",
"@types/node": "^10.12.21",
"@types/vscode": "^1.39.0",
"glob": "^7.1.4",
"mocha": "^6.1.4",
"typescript": "^3.3.1",
"tslint": "^5.12.1",
"vscode-test": "^1.2.0"
},
"repository": {
"type": "git",
"url": "https://github.com/nanlei2000/git-diff-warnning"
},
"bugs": {
"url": "https://github.com/nanlei2000/git-diff-warnning/issues"
},
"icon": "media/icon.png"
}
"name": "git-diff-warning",
"displayName": "Git Diff Warning",
"description": "Warn if git diff line count is too big",
"publisher": "nanlei2000",
"version": "1.0.2",
"engines": {
"vscode": "^1.39.0"
},
"categories": [
"Other"
],
"activationEvents": [
"workspaceContains:.git"
],
"main": "./out/extension.js",
"contributes": {
"configuration": [
{
"title": "Git Diff Warning Configuration",
"properties": {
"gitDiffWarning.maxCountEachTypeAndSum": {
"type": "array",
"default": [
null,
200,
200,
200
],
"description": "Max counts for 'changed files', 'insertions', 'deletions' and 'insertions deletions sum'.Set `null` to ignore specific one."
},
"gitDiffWarning.baseReference": {
"type": "string",
"default": "HEAD",
"description": "Define which branch, commit, tag or symbolic reference must be used as base for diff comparison. One can set 'origin/main', for instance."
}
}
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/mocha": "^5.2.6",
"@types/node": "^10.12.21",
"@types/vscode": "^1.39.0",
"glob": "^7.1.4",
"mocha": "^6.1.4",
"typescript": "^3.3.1",
"tslint": "^5.12.1",
"vscode-test": "^1.2.0"
},
"repository": {
"type": "git",
"url": "https://github.com/nanlei2000/git-diff-warnning"
},
"bugs": {
"url": "https://github.com/nanlei2000/git-diff-warnning/issues"
},
"icon": "media/icon.png"
}
11 changes: 7 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import child_process = require('child_process');
import util = require('util');
import child_process = require("child_process");
import util = require("util");
const exec = util.promisify(child_process.exec);
type ParsedRes = {
stdout: string;
Expand All @@ -8,9 +8,12 @@ type ParsedRes = {
deleteCount: number;
};

export async function getCount(path: string): Promise<ParsedRes | undefined> {
export async function getCount(
path: string,
baseReference: string
): Promise<ParsedRes | undefined> {
try {
const cmd = `git diff --shortstat`;
const cmd = `git diff --shortstat ${baseReference}`;
const { stdout } = await exec(cmd, {
cwd: path,
});
Expand Down
40 changes: 22 additions & 18 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { getCount } from './core';
import * as vscode from "vscode";
import { getCount } from "./core";
export function activate() {
const status = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
Expand All @@ -10,7 +10,7 @@ export function activate() {
return;
}
const rootPath = folders[0].uri.path;
const pattern = new vscode.RelativePattern(rootPath, '**/*');
const pattern = new vscode.RelativePattern(rootPath, "**/*");
const watcher = vscode.workspace.createFileSystemWatcher(pattern);
let isNeedWarn: boolean = false;

Expand All @@ -24,32 +24,36 @@ export function activate() {

const getCountText = (count: number, maxCount: number): string => {
if (maxCount < count) {
return count + '!';
return count + "!";
} else {
return count + '';
return count + "";
}
};

type MaxCountsConfig = [number, number, number, number];
function getMaxCountFromConfig(): MaxCountsConfig {
let config = vscode.workspace.getConfiguration('gitDiffWarning');
let config = vscode.workspace.getConfiguration("gitDiffWarning");
const maxCountEachType = config
.get<(number | null)[]>('maxCountEachTypeAndSum')!
.map(v => (v !== null ? +v : Infinity));
.get<(number | null)[]>("maxCountEachTypeAndSum")!
.map((v) => (v !== null ? +v : Infinity));
return maxCountEachType as MaxCountsConfig;
}

async function updateStatus(): Promise<void> {
const maxCountEachType = getMaxCountFromConfig();
const res = await getCount(rootPath);
let config = vscode.workspace.getConfiguration("gitDiffWarning");
const baseReference = config.get<string>("baseReference") || "HEAD";
const res = await getCount(rootPath, baseReference);
if (res) {
status.text = ([
['$(file)', res.modifiedFileCount],
['$(diff-added)', res.insertCount],
['$(diff-removed)', res.deleteCount],
] as const)
status.text = (
[
["$(file)", res.modifiedFileCount],
["$(diff-added)", res.insertCount],
["$(diff-removed)", res.deleteCount],
] as const
)
.map((v, i) => `${v[0]} ${getCountText(v[1], maxCountEachType[i])}`)
.join(' ');
.join(" ");
status.tooltip = res.stdout;
if (
[
Expand All @@ -61,14 +65,14 @@ export function activate() {
) {
if (isNeedWarn === false) {
vscode.window.showWarningMessage(
'WARNING: git diff too big, consider to commit'
"WARNING: git diff too big, consider to commit"
);
}
isNeedWarn = true;
status.color = '#f00';
status.color = "#f00";
} else {
isNeedWarn = false;
status.color = 'unset';
status.color = "unset";
}
status.show();
} else {
Expand Down