git的笔记

git init
git add 文件名
git commit -m "这是注释"
git clone 远程仓库地址
git clone 远程仓库地址 目录
git clone -b 分支名/标签 --single-branch --no-tags --depth=1  远程仓库地址 目录
--single-branch 仅克隆单一分支
--no-tags 不下载标签
--depth=1 浅层克隆(shallow clone),仅获取最近 1 个提交,而非完整历史
git pull
git push origin master
git branch 分支名
git checkout 分支名
git switch 分支名
git branch
git branch -a
git branch -D 分支名
git merge 分支名
git merge 远程仓库名/分支名
git log
只显示最近的两条记录
git log -2

git fetch; git log --author="username@163.com"  --full-history --pretty="%h %S %ce %ci %s" --date-order --decorate=full --skip=0 --after="2023-12-31" --branches --tags --remotes
git fetch; git log --author="username@163.com"  --full-history --pretty="%h %S %ce %ci %s" --date-order --decorate=full --skip=0 --after="$(date -d "$(date +%Y%m01) last day" +%Y-%m-%d)" --branches --tags --remotes
git remote add 远程仓库名 远程仓库地址
例子
git remote add test3 ssh://username@127.0.0.1//alidata/www/.git
git push 远程仓库名
git pull 远程仓库名
git remote -v
git remote rename 旧名字 新名字
git remote rm 仓库名
git status
更改->暂存的更改(add)->提交(commit)(提交暂存文件)->推送(push)
    1. git fetch:相当于是从远程获取最新版本到本地,不会自动合并。
    git fetch origin master
    git log -p master..origin/master
    git merge origin/master
以上命令的含义:
首先从远程的origin的master主分支下载最新的版本到origin/master分支上然后比较本地的master分支和origin/master分支的差别最后进行合并
上述过程其实可以用以下更清晰的方式来进行:
    $ git fetch origin master:tmp
    $ git diff tmp 
    $ git merge tmp
    2. git pull:相当于是从远程获取最新版本并merge到本地 
    git pull origin master
    上述命令其实相当于git fetch 和 git merge在实际使用中,git fetch更安全一些,
    因为在merge前,我们可以查看更新情况,然后再决定是否合并。
git config user.signingkey [用户ID]
git tag -s tagname -m 'msg'
git tag -v tagname
git show tagname
git commit -S -m 'msg'
git verify-commit commitid
git log --show-signature -10
git reset --soft HEAD~1
git commit --amend
git push -f
git reset -q HEAD -- .
git checkout -- .
git clean -fd
git reset --hard
git stash
git stash push
git stash pop
git stash apply
git stash list
git stash show
git stash drop
git stash clear

pre-commit

#!/bin/bash

# 获取暂存区中修改的文件列表
echo "获取暂存区文件..."
changed_files=$(git diff --cached --name-only)

# 检查是否有文件被修改
if [ -z "$changed_files" ]; then
    echo "没有暂存的文件"
    exit 0
fi

# 将文件列表按换行符分割成数组
IFS=
    #39;\n' read -d '' -ra files <<< "$changed_files"

# 创建一个数组来存储符合条件的文件
php_files=()

# 筛选出以 .php 和 .phtml 结尾的文件
echo "筛选 PHP 相关文件..."
for file in "${files[@]}"; do
    # 检查文件是否以 .php 或 .phtml 结尾
    if [[ "$file" == *.php ]] || [[ "$file" == *.phtml ]]; then
        # 检查文件是否存在(避免已删除的文件)
        if [ -f "$file" ]; then
            php_files+=("$file")
        fi
    fi
done

# 检查是否有符合条件的文件
if [ ${#php_files[@]} -eq 0 ]; then
    echo "没有找到 .php 或 .phtml 文件需要检测"
    exit 0
fi

# 显示要检测的文件
echo "检测以下文件:"
for file in "${php_files[@]}"; do
    echo "  - $file"
done

# 使用 phpcs 对筛选出的文件进行格式检测
echo "开始代码格式检测..."

# phpcs_result=0
# for file in "${php_files[@]}"; do
#     echo "检测文件: $file"
#     # vendor/bin/phpcs --standard=Magento2 --warning-severity=3 "$file"
#     vendor/bin/phpstan analyse --no-progress --no-ansi -l 4 "$file"
#     if [ $? -ne 0 ]; then
#         phpcs_result=1
#     fi
#     echo "------------------------"
# done
# if [ $phpcs_result -eq 0 ]; then
#     echo "所有文件格式检测通过"
# else
#     echo "发现代码格式问题,请修复后重新提交"
#     exit 1
# fi

vendor/bin/phpstan analyse --no-progress --no-ansi -l 4 "${php_files[@]}"
if [ $? -eq 0 ]; then
    echo "所有文件格式检测通过"
    exit 0
else
    echo "发现代码格式问题,请修复后重新提交"
    exit 1
fi

这是一段 调用 llm 分析 文件的脚本

#!/bin/bash

set -euo pipefail

script_name=$(basename "$0")

# 用法提示
usage() {
    cat << EOF
Usage: $script_name [OPTIONS] <file1> [file2 ...]

Options:
  -u, --url <url>           API endpoint URL (required)
  -k, --key <key>           API Key / Bearer token (required)
  -m, --model <name>        Model name (required)
  -s, --system <prompt>     System prompt text (optional)
  -h, --help                Show this help message

Examples:
  ./$script_name -u https://api.openai.com/v1/chat/completions \
              -k sk-xxxxx \
              -m gpt-4 \
              -s "You are a code reviewer" \
              file1.php file2.js

  ./$script_name -u http://localhost:11434/v1/chat/completions \
              -k "ollama" \
              -m llama3 \
              ./src/*.py
EOF
    exit 1
}

# 参数解析
URL=""
KEY=""
MODEL=""
SYSTEM_PROMPT=""
FILES=()

while [[ $# -gt 0 ]]; do
    case "$1" in
        -u|--url)
            URL="${2:-}"
            shift 2
            ;;
        -k|--key)
            KEY="${2:-}"
            shift 2
            ;;
        -m|--model)
            MODEL="${2:-}"
            shift 2
            ;;
        -s|--system)
            SYSTEM_PROMPT="${2:-}"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        -*)
            echo "Error: Unknown option $1" >&2
            usage
            ;;
        *)
            FILES+=("$1")
            shift
            ;;
    esac
done

# 参数校验
[[ -z "$URL" ]] && { echo "Error: URL is required (-u)" >&2; usage; }
[[ -z "$KEY" ]] && { echo "Error: API Key is required (-k)" >&2; usage; }
[[ -z "$MODEL" ]] && { echo "Error: Model name is required (-m)" >&2; usage; }
[[ ${#FILES[@]} -eq 0 ]] && { echo "Error: At least one input file is required" >&2; usage; }

# 检查文件是否存在
for f in "${FILES[@]}"; do
    if [[ ! -f "$f" ]]; then
        echo "Error: File not found: $f" >&2
        exit 1
    fi
done

for f in "${FILES[@]}"; do
    echo "$f"
done

# exit 1;

# ── JSON 转义辅助函数 ──
# 优先使用 jq,其次尝试 python3,最后回退到基础 sed(仅处理常见字符)
json_escape() {
    local text="$1"
    if command -v jq &>/dev/null; then
        printf '%s' "$text" | jq -Rs '.[:-1]'
    elif command -v python3 &>/dev/null; then
        python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()), end="")' <<< "$text"
    else
        # 基础转义(处理引号、反斜杠、换行、制表符)
        printf '%s' "$text" | sed \
            -e 's/\\/\\\\/g' \
            -e 's/"/\\"/g' \
            -e 's/\t/\\t/g' \
            -e 's/\r/\\r/g' \
            -e ':a;N;$!ba;s/\n/\\n/g'
    fi
}

# ── 读取所有文件并合并为 user message ──
USER_CONTENT=""
for f in "${FILES[@]}"; do
    [[ -n "$USER_CONTENT" ]] && USER_CONTENT+=
    #39;\n\n'
    USER_CONTENT+="==== FILE: $(basename "$f") ===="
    #39;\n'
    USER_CONTENT+=$(cat "$f")
done

# echo "$USER_CONTENT"

USER_CONTENT=$SYSTEM_PROMPT
    #39;\n\n'$USER_CONTENT
# echo "$USER_CONTENT"

PAYLOAD=$(jq -n \
    --arg model "$MODEL" \
    --arg prompt "$USER_CONTENT" \
'{
    model: $model,
    prompt: $prompt,
    temperature: 0.7,
    stream: false
}')

# echo "$PAYLOAD"
# exit 1;

RESPONSE=$(curl -s -w "\n%{http_code}" \
    -H "Content-Type: application/json;charset=utf-8" \
    -H "Authorization: Bearer $KEY" \
    -d "$PAYLOAD" \
    "$URL")

HTTP_CODE=$(tail -n1 <<< "$RESPONSE")
BODY=$(sed '$ d' <<< "$RESPONSE")

# echo $BODY;

AI_RESULT=$(echo $BODY | jq '.choices[0].text')

if [ "$AI_RESULT" = "success" ]; then
    echo "agent 没有发现问题"
    exit 0
fi

echo $AI_RESULT

exit 1;

类似这样调用

./mrico.sh -u http://localhost/api/v1/completions \
            -k auth-key \
            -m "deepseek/deepseek-v3" \
            -s "你是一个 code reviewer ,请从 语法,性能 和 安全 角度分析以下文件;如果所有文件都没有问题请直接输出 success ,如果有问题请按以下格式逐行输出 file:lie problem" \
            template.phtml index.php