【问题标题】:How to get modified files from Git within a VSCode extension如何在 VSCode 扩展中从 Git 获取修改后的文件
【发布时间】:2021-08-30 18:39:20
【问题描述】:

作为练习,我正在尝试制作一个类似于现有 VScode 源代码控制选项卡的 VScode 扩展。

类似于下面的截图,我想获取当前项目中所有被修改的文件的列表(运行“git status”时显示的文件列表),然后在树视图中显示它们。

如何使用 Typescript 和 VSCode API 查询 git 以获取所有修改文件的列表?

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    在这里,这可能对source-control-sample 有所帮助。还可以查看vscode-extension-samples,他们在制作 vscode 扩展时真的很有帮助

    【讨论】:

      【解决方案2】:

      要访问 VS Code 工作区中的 Git Repos,您可以利用与 VS Code 捆绑在一起的 Git 扩展提供的 API。

      这是一个示例 extension.ts 文件,用于转储工作区中第一个 repo 的更改数组:

      import * as vscode from "vscode";
      import {  GitExtension } from "./git";
      
      
      export async function activate(context: vscode.ExtensionContext) {
      
        // Run code when command is triggered. Requires to define a command in package.json...
        // This is one way to make sure, that all other extensions -- especially the Git extension -- have been activated, too.
        // Otherwise getExtension() might return `undefined`.
        let disposable = vscode.commands.registerCommand("myExtension.myCommandId", async () => {
            
            //Get access to API provided by Git extension that is always part of VS Code
            const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git')!.exports;
            const git = gitExtension.getAPI(1);
      
            //Get list of repositories available in current VS Code workspace
            const repos = git.repositories;
      
            //Get all changes for first repository in list
            const changes = await repos[0].diffWithHEAD();
            
            //Print out array of changes
            console.info(changes);
        });
      
        context.subscriptions.push(disposable);
      }
      
      
      export function deactivate() {}
      
      

      您可以在这篇文章中找到其他示例:How to access the api for git in visual studio code

      【讨论】:

        猜你喜欢
        • 2019-05-04
        • 2019-01-05
        • 1970-01-01
        • 2018-09-09
        • 1970-01-01
        • 2018-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多