Skip to content

Commit

Permalink
change name to git changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzg62 committed Oct 8, 2023
1 parent 699acaa commit 5bae60e
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"cgcf"
"cgcf",
"jazzg"
]
}
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Copy Changes
# Git Changes

这个拓展可以让你复制 git 项目中有改动的部分,包含文件和文件夹
这个拓展可以让你查看 git 项目中有改动的部分,包含文件和文件夹

This extension allows you to copy the part of the project that has been changed, including files and folders.
This extension allows you to view changed parts of your git project, including files and folders.

## 如何使用

打开侧边栏 Source Control, 右键点击更改的文件,选择 Copy Changes
打开侧边栏 Source Control, 右键点击更改的文件,选择 Show Changes

在成功复制后,会自动打开存储所有改动文件的临时文件夹

![demo](https://jazzg62.github.io/copy-changes/demo.gif)
![demo](./docs/demo.gif)

## 特性

复制 git 项目中改动的文件和文件夹
查看 git 项目中改动的文件和文件夹

支持选择部分改动复制
支持选择部分改动查看

## 已知问题

目前仅支持 windows

改动的文件名中含有中文的,可能会出现复制失败的情况,可尝试运行以下命令修复。
改动的项目中含有中文的,可能会出现失败的情况,可尝试运行以下命令修复。

```bash
git config --global core.quotepath false
Expand Down
34 changes: 17 additions & 17 deletions dist/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
// 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_1 = __importDefault(require("vscode"));
const cgcf = __importStar(require("./cgcf"));
const utils = __importStar(require("./utils"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
// this method is called when your extension is activated
Expand All @@ -42,10 +42,10 @@ const target_path = path_1.default.join(workspaceRoot, tmp);
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let copyChanges = vscode_1.default.commands.registerCommand('cc.copyChanges', function () {
let showChanges = vscode_1.default.commands.registerCommand('cc.showChanges', function () {
console.log(target_path);
cgcf.clear(target_path);
let changes = cgcf.getGitRepoChanges(workspaceRoot);
utils.clear(target_path);
let changes = utils.getGitRepoChanges(workspaceRoot);
console.log(changes);
if (changes.length === 0) {
vscode_1.default.window.showWarningMessage('无文件改动!');
Expand All @@ -57,15 +57,15 @@ function activate(context) {
source_file = path_1.default.resolve(workspaceRoot, item);
target_file = path_1.default.join(target_path, item);
count++;
cgcf.copy(source_file, target_file);
utils.copy(source_file, target_file);
}
if (cgcf.openInExplorer(target_path))
vscode_1.default.window.showInformationMessage(`拷贝成功!共${count}个项目`);
if (utils.openInExplorer(target_path))
vscode_1.default.window.showInformationMessage(`成功!共${count}个项目`);
else
vscode_1.default.window.showInformationMessage(`拷贝出错!`);
vscode_1.default.window.showInformationMessage(`出现错误!`);
});
let copySelectChanges = vscode_1.default.commands.registerCommand('cc.copySelectedChanges', function () {
cgcf.clear(target_path);
let showSelectChanges = vscode_1.default.commands.registerCommand('cc.showSelectedChanges', function () {
utils.clear(target_path);
let changes = arguments;
if (changes.length === 0) {
vscode_1.default.window.showWarningMessage('无文件改动!');
Expand All @@ -82,24 +82,24 @@ function activate(context) {
continue;
target_file = source_file.replace(workspaceRoot, target_path);
count++;
cgcf.copy(source_file, target_file);
utils.copy(source_file, target_file);
}
if (count == 0) {
vscode_1.default.window.showWarningMessage('无新增或修改的文件!');
return;
}
if (cgcf.openInExplorer(target_path))
vscode_1.default.window.showInformationMessage(`拷贝成功!共${count}个项目`);
if (utils.openInExplorer(target_path))
vscode_1.default.window.showInformationMessage(`成功!共${count}个项目`);
else
vscode_1.default.window.showInformationMessage(`拷贝出错!`);
vscode_1.default.window.showInformationMessage(`出现错误!`);
});
context.subscriptions.push(copyChanges);
context.subscriptions.push(copySelectChanges);
context.subscriptions.push(showChanges);
context.subscriptions.push(showSelectChanges);
}
// this method is called when your extension is deactivated
function deactivate() {
if (fs_1.default.existsSync(target_path))
cgcf.clear(target_path);
utils.clear(target_path);
}
module.exports = {
activate,
Expand Down
135 changes: 135 additions & 0 deletions dist/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.openInExplorer = exports.getGitRepoChanges = exports.clear = exports.parse = exports.log = exports.copyFolder = exports.copy = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
/**
* 复制文件
*/
function copy(src, dst) {
// 处理文件夹
if (fs_1.default.statSync(src).isDirectory()) {
fs_1.default.mkdirSync(dst, { recursive: true });
copyFolder(src, dst);
return;
}
// 处理文件
const dirname = path_1.default.dirname(dst);
// 创建对应的文件夹
if (!fs_1.default.existsSync(dst))
fs_1.default.mkdirSync(dirname, { recursive: true });
fs_1.default.copyFileSync(src, dst);
}
exports.copy = copy;
/**
* 复制文件夹
*/
function copyFolder(srcDir, desDir) {
fs_1.default.readdir(srcDir, { withFileTypes: true }, (err, files) => {
for (const file of files) {
//判断是否为文件夹
if (file.isDirectory()) {
const dirS = path_1.default.resolve(srcDir, file.name);
const dirD = path_1.default.resolve(desDir, file.name);
//判断是否存在dirD文件夹
if (!fs_1.default.existsSync(dirD)) {
fs_1.default.mkdir(dirD, (err) => {
if (err)
console.log(err);
});
}
copyFolder(dirS, dirD);
}
else {
const srcFile = path_1.default.resolve(srcDir, file.name);
const desFile = path_1.default.resolve(desDir, file.name);
fs_1.default.copyFileSync(srcFile, desFile);
}
}
});
}
exports.copyFolder = copyFolder;
/**
* 打印日志
*/
function log() {
console.log(...arguments);
}
exports.log = log;
/**
* 解析diff文本
*/
function parse(statusText) {
let arr = statusText.split("\n");
let res = [];
arr.forEach((el) => {
if (el.startsWith("\t")) {
res.push(el);
}
});
let del = [];
for (let i in res) {
if (/deleted: /.test(res[i]))
del.push(Number(i));
// 去掉\t
res[i] = res[i].replace(/\t/g, "");
// 去掉 modified
res[i] = res[i].replace(/modified: /g, "");
// 去掉 new file
res[i] = res[i].replace(/new file: /g, "");
// 去除 ../
res[i] = res[i].replace(/\.\.\//g, "");
// 去掉 renamed
if (/renamed: /.test(res[i])) {
res[i] = res[i].replace(/renamed: /g, "");
let d = res[i].split('->');
res[i] = d[1].trim();
}
}
// 处理删除的文件
let t = 0;
for (let i in del) {
res.splice(del[i] - t, 1);
t++;
}
return res;
}
exports.parse = parse;
/**
* 清空路径下的文件
*/
function clear(path) {
if (!fs_1.default.existsSync(path))
return;
try {
fs_1.default.rmSync(path, { recursive: true, force: true });
}
catch (e) {
console.warn(e);
}
}
exports.clear = clear;
/**
* 获取git项目的改变文件列表
*/
function getGitRepoChanges(path) {
if (!fs_1.default.existsSync(path))
return [];
let data = (0, child_process_1.execSync)(`cd /D ${path} && git status`).toString();
return parse(data);
}
exports.getGitRepoChanges = getGitRepoChanges;
/**
* 在文件浏览器中打开
*/
function openInExplorer(path) {
if (!fs_1.default.existsSync(path))
return false;
(0, child_process_1.execSync)(`start "" "${path}"`);
return true;
}
exports.openInExplorer = openInExplorer;
Binary file modified docs/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 4 additions & 25 deletions package-lock.json

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

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "CopyChanges",
"displayName": "Copy Changes",
"description": "copy git changes file",
"version": "0.2.2",
"name": "GitChanges",
"displayName": "Git Changes",
"description": "show git changes file",
"version": "0.3.0",
"author": "https://github.com/jazzg62",
"publisher": "jazzg62",
"icon": "logo.png",
Expand All @@ -17,32 +17,32 @@
"Other"
],
"activationEvents": [
"onCommand:cc.copyChanges",
"onCommand:cc.copySelectedChanges"
"onCommand:cc.showChanges",
"onCommand:cc.showSelectedChanges"
],
"main": "./dist/extension.js",
"contributes": {
"commands": [
{
"command": "cc.copyChanges",
"title": "Copy Changes",
"command": "cc.showChanges",
"title": "Show Changes",
"when": "scmProvider == git"
},
{
"command": "cc.copySelectedChanges",
"title": "Copy Select Changes",
"command": "cc.showSelectedChanges",
"title": "Show Select Changes",
"when": "scmProvider == git"
}
],
"menus": {
"scm/resourceState/context": [
{
"command": "cc.copyChanges",
"command": "cc.showChanges",
"group": "navigation",
"when": "scmProvider == git"
},
{
"command": "cc.copySelectedChanges",
"command": "cc.showSelectedChanges",
"group": "navigation",
"when": "scmProvider == git"
}
Expand Down
Loading

0 comments on commit 5bae60e

Please sign in to comment.