【问题标题】:Git command to determine if a directory will be present in a clone after running commit?Git命令确定运行提交后目录是否存在于克隆中?
【发布时间】:2014-08-07 12:35:47
【问题描述】:

我正在编写一个 git pre commit 钩子(用于 Windows 和 osx),如果我在提交当前暂存的 mod 后克隆 repo,我需要知道是否会存在一个目录。由于没有跟踪目录,我需要知道在提交后是否有一个文件将在目录或其任何子目录中被跟踪。我目前使用以下代码,但感觉很不稳定:

public bool TrackedAfterCommit(IDirectory directory)
{
    var dirs = new Stack<IDirectory>();
    dirs.Push(directory);

    while (dirs.Count > 0)
    {
        var d = dirs.Pop();

        foreach (var f in d.Files)
        {
            var command = new ConsoleCommand(
                "git",
                "status --porcelain --ignored " + f.Path, directory.Path
            );
            var output = command.Execute();

            if (output.Count == 0)
                return true;

            var status = output[0];

            if (!status.StartsWith("D")
                && !status.StartsWith("??")
                && !status.StartsWith("!"))
            {
                return true;
            }
        }

        foreach (var dir in d.Directories)
            dirs.Push(dir);
    }

    return false;
}

【问题讨论】:

    标签: git githooks pre-commit-hook


    【解决方案1】:

    您可以使用以下方法获取分支BRANCH 上包含 git 控制文件的所有目录的列表:

    git ls-tree -r --name-only --full-tree BRANCH | xargs -n 1 dirname | sort -u
    

    【讨论】:

    • 抱歉,忘了说它必须在windows(和osx)上工作,最好没有额外的依赖
    • 如果你有适用于 Windows 的 Git,这在 Git Bash 中可以正常工作。 Mac OS X 类似(除非它们的 BSD 工具在命令行选项上再次与 GNU 不同)。
    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2012-08-12
    相关资源
    最近更新 更多