gpt4 book ai didi

linux - git:确定是否有文件不在 repo 协议(protocol)中,但应该添加

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:05 24 4
gpt4 key购买 nike

我有一个仓库,其中 Git 显示:

$ git status
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: pom.xml
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: src/main/java/fr/SomeClass.java
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# README.md
#

如果出现以下情况,我想以相当快的方式知道:

  • 有准备提交的文件(此处:pom.xml)
  • 未准备提交(此处:SomeClass.java)
  • 未跟踪的文件(此处:README.md)

我在第一种情况下使用以下命令:

    git diff --no-ext-diff --quiet --exit-code       2> /dev/null || w='*'
git diff --no-ext-diff --quiet --exit-code HEAD 2> /dev/null || h='+'

但是只有一个文件没有暂存时,wh*+填充。

$ git status --porcelain
M pom.xml
M src/main/java/fr/SomeClass.java
?? toto

会工作,但它会打印很多,我正在寻找可以简单地打印多个项目的东西,例如:

1 1 1

其中每个数字分别是暂存的、未暂存的和未跟踪的。

我在这里的目的是打印我的存储库是否发生了变化,以及发生了什么变化。我不想解析整个输出(这将适用于 grep 等,但我想避免这种情况)。

哦,我将它标记为 Windows,因为我正在寻找一种既能在 Linux 上工作又能在 Windows 上使用 git-bash 的解决方案。基于 Bash 4 的解决方案,如果没有明确的方法在 Bash 3 中执行此操作,对我帮助不大。

最佳答案

#!/bin/bash

let untracked_count=0 worktree_count=0 staged_count=0 other_count=0
git status -z --porcelain > status.tmp
while IFS= read -n 2 stat; IFS= read -d '' line; do
case "$stat" in
(\?\?) (( untracked_count++ )) ;;
(\ ?) (( worktree_count++ )) ;;
(?\ ) (( staged_count++ )) ;;
(M?) (( worktree_count++ )) ; (( staged_count++ )) ;;
*) (( other_count++ )) ;;
esac
done < status.tmp

阅读git status -z --porcelain使用 bash 内置输出 read获取我们关心的信息,然后计算我们看到的内容。 IFS在第一次读取时,有必要避免 shell 转储前导或尾随空格。 IFS在这种情况下,第二次读取不是绝对必要的,而是我使用的“按行读取输入”模式。

使用 bash 4 将减少重复的 case 标签集(通过使用 ;;& )。

旁注:在 Linux 上,可以这样做:

while IFS= read -n 2 stat; IFS= read -d '' line; do
..
done < <(git status -z --porcelain)

但是 <(...) Windows 上的 Git Bash 中未实现重定向。

关于linux - git:确定是否有文件不在 repo 协议(protocol)中,但应该添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25442356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com