git change email by filter-branch/set local git email

示例展示了如何使用Git配置别名及filter-branch命令,修改从某个提交点到HEAD之间的所有提交的作者邮箱,将'lake@localhost'替换为'lake.hu@outlook.com'。操作后,Git日志显示作者邮箱已更新,但提醒使用git-filter-repo代替git-filter-branch以避免历史重写问题。
[lake@localhost linux-stable]$ git config --global alias.change-commits '!'"f() { VAR=\$1; OLD=\$2; NEW=\$3; shift 3; git filter-branch --env-filter \"if [[ \\\"\$\`echo \$VAR\`\\\" = '\$OLD' ]]; then export \$VAR='\$NEW'; fi\" \$@; }; f"

[lake@localhost linux-stable]$ cat  ~/.gitconfig
[user]
        name = Lake Hu
        email = lake@localhost
[alias]
[alias]
    
        change-commits = "!f() { VAR=$1; OLD=$2; NEW=$3; shift 3; git filter-branch --env-filter \"if [[ \\\"$`echo $VAR`\\\" = '$OLD' ]]; then export $VAR='$NEW'; fi\" $@; }; f"
[kreatv]
        autoFormatSourceCode = true
[core]
        editor = vim
        autocrlf = input
[color]
        ui = auto
[lake@localhost linux-stable]$



Example:

[lake@localhost linux-stable]$ git log
commit 38b45955d1e2d33a43a7c0d4b03c295fbd0a9f38 (HEAD -> brcm_4.9.132)
Author: Lake Hu <lake@localhost>
Date:   Mon Nov 16 14:50:45 2020 +0800

    patch and kernel config

commit 63f0232cfc58c7fb19ea02cdd527f9a09193f37b
Author: Lake Hu <lake@localhost>
Date:   Mon Nov 16 14:45:01 2020 +0800

    4.9-1.10.tar.bz2, Dec 14 2018

commit 3622426cc93ab5bc75fa9cf2e897441dff894c34 (tag: v4.9.132)
Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Date:   Wed Oct 10 08:53:23 2018 +0200

    Linux 4.9.132

 

[lake@localhost linux-stable]$ git change-commits GIT_AUTHOR_EMAIL "lake@localhost" "lake.hu@outlook.com" HEAD~3..HEAD
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.



Proceeding with filter-branch...


Rewrite 38b45955d1e2d33a43a7c0d4b03c295fbd0a9f38 (3/3) (0 seconds passed, remaining 0 predicted)
Ref 'refs/heads/brcm_4.9.132' was rewritten
[lake@localhost linux-stable]$
[lake@localhost linux-stable]$
[lake@localhost linux-stable]$
[lake@localhost linux-stable]$
[lake@localhost linux-stable]$ git log
commit 92ba1220faf2a11b57b1f93abdad80e627c5dd47 (HEAD -> brcm_4.9.132)
Author: Lake Hu <lake.hu@outlook.com>		   ------------>  Changed 
Date:   Mon Nov 16 14:50:45 2020 +0800             ------------>  No change!!! 

    patch and kernel config

commit 193b8f69e010728a6028ba1971c872d3dd00f364
Author: Lake Hu <lake.hu@outlook.com>
Date:   Mon Nov 16 14:45:01 2020 +0800

    4.9-1.10.tar.bz2, Dec 14 2018

commit 3622426cc93ab5bc75fa9cf2e897441dff894c34 (tag: v4.9.132)
Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Date:   Wed Oct 10 08:53:23 2018 +0200

    Linux 4.9.132

##############  set  local git email 

[lake@localhost linux-stable]$ git config user.email
lake@localhost


[lake@localhost linux-stable]$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
        fetch = +refs/heads/*:refs/remotes/origin/*
		
[lake@localhost linux-stable]$ git config user.email   lake.hu@outlook.com
[lake@localhost linux-stable]$ git config user.email
lake.hu@outlook.com
[lake@localhost linux-stable]$

[lake@localhost linux-stable]$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[user]
        email = lake.hu@outlook.com
[lake@localhost linux-stable]$


[apply] ignorewhitespace = change whitespace = nowarn [push] default = upstream rebase = true followTags = true [color] interactive = true branch = true status = true [core] mergeoptions = --no-edit pager = less -FiR # -F is necessary but has problems with tmux and regular shells #pager = most excludesfile = /home/braulio/.gitignore #pager = vimpager #pager = more -p [mergetool "vimdiff3"] cmd = vim -u ~/.vimrc -f -d -c \"wincmd J\" \"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\" [alias] root = rev-parse --show-toplevel #cdroot = !cd `git root` out = log --branches --not --remotes in = pull --dry-run co = checkout coc = checkout . cob = checkout -b cobf = "!f() { git brd $1; git cob $1; }; f" col = checkout HEAD~1 cotc = !git sttc | xargs git checkout como = !git stmo | xargs git checkout ours = checkout --ours theirs = checkout --theirs aours = "!f() { (git reset $* >/dev/null); git ours $* && git add $*; }; f" atheirs = "!f() { (git reset $* >/dev/null); git theirs $* && git add $*; }; f" ci = commit cif = "!f() { vim $1; git ci $1; }; f" cie = commit --allow-empty cia = commit -a cam = commit --amend cama = commit --amend -a cl = clean cldf = clean -df res = reset resa = reset HEAD resh = "!f() { git stash && git reset --hard $@; }; f" reshu = !git resh `git upstream` reshfu = !git fetch `git upstream-remote` && git resh `git upstream` reshh = !git stash && git reset --hard HEAD cundo = reset HEAD^1 cundoh = !git stash && git reset HEAD^1 --hard unstage = reset HEAD tags = tag -l pg = diff pgs = diff --staged pa = apply s = show --color sw = show --color --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+' sf = show --color -C --stat d = diff --color dw = diff --color --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+' du = !git d `git upstream` $@ ds = !git d --staged $@ dsf = !git d --staged --stat $@ dsw = !git d --staged --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+' $@ dsf = !git d --staged -C --stat $@ df = !git d -C --stat $@ dc = "!f() { HASH=$1; shift 1; git diff --cc $HASH $HASH^1 $HASH^2 $@; }; f" lp = log -m --color -p lf = log -m --color -C --stat lg = log -m --simplify-merges --color --graph --pretty=format:'%Cred%h%Creset %s %Cgreen(%ar) %Cblue%an <%ae>%Creset' --abbrev-commit --date=relative lgu = !git lg `git upstream` $@ lg2 = !source ~/.githelpers && pretty_git_log lgp = !git lg -p $@ lgpw = !git lg -p --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+' $@ lgf = !git lg -C --stat $@ l = log -m --simplify-merges --color --pretty=format:'%Cred%h%Creset %s %Cgreen(%cr) %Cblue%an <%ae>%Creset' --abbrev-commit --date=relative lc = "!f() { git log $* | grep '^commit ' | cut -f 2 -d ' '; }; f" lgu = !git lg --no-merges `git upstream` $@ lgup = !git lg --no-merges -p `git upstream` $@ f = fetch fu = !git fetch `git upstream-remote` $@ fa = fetch --all fo = fetch origin m = merge --no-edit unm = reset --merge ps = push psu = push -u psn = "!f() { git ps $1 `git brc`; }; f" psnf = "!f() { git psf $1 `git brc`; }; f" psfun = "!f() { git psu -f $1 `git brc`; }; f" psun = "!f() { git psu $1 `git brc`; }; f" pso = push origin psoun = !git psu origin `git brc` $@ psf = push -f psuf = push -uf pssm = push --recurse-submodules=on-demand psremoveallbranches = "!f() { git push $1 `git brar $1 | grep -v master | xargs -I {} echo :{}`; }; f" pl = pull plo = pull origin plom = pull origin master plr = !git sh && git pull --rebase && git shp $@ st = status sttc = !git status | grep typechange | awk '{print $2}' $@ stmo = !git status | grep modified | awk '{print $2}' $@ a = add --all af = add --all -f ap = add -p resolve-unmerged = !git add `git unmerged` $@ add-untracked = !git add `git untracked` $@ as = !git diff -w --no-color | git apply --cached $@ rs = !(git resa && git as && git co . && git resa) &>/dev/null rmf = rm -rf rmtheirs = !git rm `git removedbytheirs` $@ rmours = !git rm `git removedbyours` $@ unmerged = !git ls-files --unmerged | cut -f2 | uniq theirsunmerged = !git ls-files --unmerged | grep \"\\\\s3\\\\s\" | cut -f2 | uniq oursunmerged = !git ls-files --unmerged | grep \"\\\\s2\\\\s\" | cut -f2 | uniq removedbyours = !bash -c 'source ~/.githelpers && diff_array \"`git theirsunmerged`\" \"`git oursunmerged`\"' removedbytheirs = !bash -c 'source ~/.githelpers && diff_array \"`git oursunmerged`\" \"`git theirsunmerged`\"' untracked = ls-files --other --exclude-standard staged = ls-files --staged modified = ls-files --modified deleted = ls-files --deleted gitignored = ls-files -o -i --exclude-standard sig = !git ls-files -v | grep "^[[:lower:]]" ig = update-index --assume-unchanged uig = update-index --no-assume-unchanged ignore = update-index --assume-unchanged ignored = !git sig $@ gitignore-untracked = !git gitignore `git untracked` $@ ig-untracked = !git ignore `git untracked` $@ gitignore = !cd `git root` && touch .gitignore head = !git l -1 $@ h = !git head $@ hp = !source ~/.githelpers && show_git_head r = !git l -20 $@ ra = !git r --all $@ sh = stash shp = stash pop shl = stash list shd = stash drop shc = stash clear sha = stash apply chp = cherry-pick chpc = cherry-pick --continue chpq = cherry-pick --quit chpa = cherry-pick --abort rv = revert rvc = revert --continue rva = revert --abort am3 = am -3 amr = am --resolved amc = am --continue ama = am --abort ams = am --skip fm = format-patch rb = rebase rbad = rebase --committer-date-is-author-date rbi = rebase -i rbtc = !git theirs `git unmerged` || git resolve-unmerged && (git rbc || git rbs) rboc = !git ours `git unmerged` || git reset `git unmerged` && git a . && (git rbc || git rbs) rbc = rebase --continue rba = rebase --abort rbs = rebase --skip squash = !"f() { NL=$1; GIT_EDITOR=\"sed -i '2,$NL s/pick/squash/;/# This is the 2nd commit message:/,$ {d}'\"; git rebase -i HEAD~$NL; }; f" sq = !git squash $1 sqpsf = !git squash $1 && git psf bs = bisect bsr = bisect reset bsb = bisect bad bsg = bisect good mt = mergetool vm = !vim `git unmerged` vmo = !vim `git modified` vt = !vim `git untracked` vim-unmerged = !git vm # deprecated current = rev-parse --abbrev-ref HEAD br = branch brc = rev-parse --abbrev-ref HEAD brt = branch --track brup = branch --set-upstream bra = !git branch -a | less -FiRS brrecent = !git for-each-ref --sort=-committerdate refs/heads/ brar = "!f() { git bra | grep remotes/$1/ | cut -f 3 -d '/'; }; f" brd = branch -D brco = branch --contains brcommits = "!f() { git lc --branches=$1* }; f" brdiff = !source ~/.githelpers && diff_branches brfdiff = !source ~/.githelpers && diff_branches_show_files rem = remote rema = remote add remr = remote rm remv = remote -v remotes = remote -v sm = submodule smst = submodule --status sma = submodule add smu = submodule update sme = submodule foreach smepl = submodule foreach "(git checkout master; git pull)&" smui = submodule update --init smuir = submodule update --init --recursive smd = submodule deinit sms = submodule sync smpl = submodule foreach git pull origin master bl = blame gcp = gc --prune=now gca = gc --aggressive gcap = gc --aggressive --prune=now # git change-commits GIT_COMMITTER_NAME "old name" "new name" change-commits = "!f() { VAR=$1; OLD=$2; NEW=$3; shift 3; git filter-branch --env-filter \"if [[ \\\"$`echo $VAR`\\\" = '$OLD' ]]; then export $VAR='$NEW'; fi\" $@; }; f " # from https://help.github.com/articles/remove-sensitive-data remove-file = "!f() { git filter-branch -f --index-filter \"git rm --cached --ignore-unmatch $1\" --prune-empty --tag-name-filter cat -- --all; }; f" rf = reflog rfe = reflog expire --expire=now --all pr = prune prm = !git rfe && git gcp fsize = git ls-tree -r -t -l --full-name HEAD | sort -rn -k 4 | less findemptydirs = !"find . -type d -empty ! -path ./tmp/\\*" deleteemptydirs = !"find . -type d -empty ! -path ./tmp/\\* -exec rmdir {} \\;" ### specifics mps = "!f() { cd ../$1; git m master; git ps; }; f" cmps = "!git mps cirandas" bmps = "!git mps blogoosfero" nghs = "!git f noosfero; git ps ghnoosfero noosfero/master:master" [url "https://"] insteadOf = git://
07-28
#!/bin/bash # ================ 配置区 ================ GERRIT_USER="longyufei" INPUT_FILE="deco_webpages.txt" # 原始大仓库(要拆分的那个) GERRIT_ORI_HOST="gerrit.cp.tp-link.com" GERRIT_ORI_PORT=29418 GERRIT_MAIN_PLATFORM="Deco/package/webpages" # 原始完整仓库名 GERRIT_MAIN_DIR="webpages" #原始仓库下载后的目录 GERRIT_REPO_OWNER="${GERRIT_MAIN_PLATFORM%/*}" # 提取 "Deco" GERRIT_REPO_NAME="${GERRIT_MAIN_PLATFORM##*/}" # 提取 "iplatform" GERRIT_SSH_URL="ssh://${GERRIT_USER}@${GERRIT_ORI_HOST}:${GERRIT_ORI_PORT}/${GERRIT_MAIN_PLATFORM}" # gerrittese GERRIT_HOST="gerrittest.tp-link.com" PORT=29418 IP="10.176.69.131:29418" # AWS TEST # 输入文件 # ================ 结束配置区 ================ echo "git clone gerrit: $GERRIT_SSH_URL" git clone "$GERRIT_SSH_URL" && \ echo "下载 Gerrit commit-msg hook..." && \ scp -p -P "$GERRIT_ORI_PORT" "${GERRIT_USER}@${GERRIT_ORI_HOST}:hooks/commit-msg" "${GERRIT_REPO_NAME}/.git/hooks/" cd $GERRIT_MAIN_DIR for r in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/); do b=${r#origin/} if ! git show-ref --verify --quiet "refs/heads/$b"; then git branch "$b" "origin/$b" fi done cd .. echo "start split" # 2)拆分ipf和特定机型的sdk,流程类似 # 先克隆一个本地仓库tmp1供拆,并创建对应的远端仓库,该仓库信息需位于txt的末尾 # 然后基于ipf.txt/sdk.txt拆解子仓库:创建远端仓库、克隆本地仓库tmp2、拆解出子仓库并提交至远端、拆解tmp1仓库的对应部分 # 最后提交tmp1仓库远端,该仓库拆解完毕 set -uo pipefail # Remove -e so script does not exit on error sed -i 's/\r$//' "$INPUT_FILE" # Read the whole file into a variable filedata=$(<"$INPUT_FILE") # Split into blocks on blank lines IFS='' blocks=() block="" while IFS= read -r line || [[ -n "$line" ]]; do if [[ -z "$line" ]]; then if [[ -n "$block" ]]; then blocks+=("$block") block="" fi else if [[ -z "$block" ]]; then block="$line" else block="$block"$'\n'"$line" fi fi done <<< "$filedata" # Add last block if missing a trailing blank line if [[ -n "$block" ]]; then blocks+=("$block") fi # the new big repo msg <-> tmp1 OLD_REPO=$(sed -n '$p' <<< "${blocks[-1]}") SDK_REPO=$(sed -n '1p' <<< "${blocks[-1]}") SDK_DESC=$(sed -n '2,5p' <<< "${blocks[-1]}") REPO_DIR=$(sed -n '6p' <<< "${blocks[-1]}") TMP_DIR_1="tmp1" # the new big remote repo echo -e "repo_name: $OLD_REPO" output=$(ssh -p "$PORT" "$GERRIT_USER@$GERRIT_HOST" \ "gerrit create-project \"$SDK_REPO\" --description \"$SDK_DESC\" --submit-type MERGE_IF_NECESSARY" 2>&1) # the new big local repo TMP_DIR_1="tmp1" rm -rf "$TMP_DIR_1" git clone --no-local "$OLD_REPO" "$TMP_DIR_1" >/dev/null # the sub repos splitted echo "==== start delete sub-repos ====" if [[ ${#blocks[@]} -gt 0 ]]; then all_but_last=("${blocks[@]:0:${#blocks[@]}-1}") for block in "${all_but_last[@]}"; do repo=$(sed -n '1p' <<< "$block") desc=$(sed -n '2,5p' <<< "$block") localpath=$(sed -n '6p' <<< "$block") OLD_PATH=$(echo "$localpath" | sed "s#^${REPO_DIR}/##") if [[ -n "$repo" && -n "$desc" ]]; then # build sub repo with desc echo "Creating repository: $repo" echo "==== DESCRIPTION START ====" echo -e "$desc" echo "==== DESCRIPTION END ====" # Temporarily disable exit on error for this command set +e output=$(ssh -p "$PORT" "$GERRIT_USER@$GERRIT_HOST" \ "gerrit create-project \"$repo\" --description \"$desc\" --submit-type MERGE_IF_NECESSARY" 2>&1) status=$? set -e if [[ $status -ne 0 ]]; then if [[ "$output" == *"already exists"* ]]; then echo "?? Repo already exists, skipped: $repo" else echo "?? Failed to create: $repo" echo "$output" fi else echo "? Created: $repo" # split the local repo and pull fi echo "===== SPLIT START =====" # clone the local repo and split to the target sub-repo TMP_DIR_2="tmp2" rm -rf "$TMP_DIR_2" echo "===== SPLIT OLD_REPO:$OLD_REPO OLD_PATH:$OLD_PATH =====" git clone --no-local "$OLD_REPO" "$TMP_DIR_2" >/dev/null cd "$TMP_DIR_2" git filter-repo --subdirectory-filter "$OLD_PATH" echo "==== SPLIT END ====" # # modify the auther-name and auther-email # git filter-repo --force --commit-callback ' # commit.author_name=b"xxx" # commit.author_email=b"xxx@example.com" # ' # push the target sub-repo to origin git remote add origin ssh://"$GERRIT_USER@$IP/$repo" 2>/dev/null || \ git remote set-url origin ssh://"$GERRIT_USER@$IP/$repo" git push origin --all cd ../ # delete the sub-repo from the big repo echo "==== REPO delete $repo start ====" cd "$TMP_DIR_1" git filter-repo --path "$OLD_PATH" --invert-paths echo "==== REPO delete $repo end ====" cd ../ fi done else echo "没有找到任何文本块" fi # push the new big repo to origin echo "==== NEW REPO PUSH START ====" cd "$TMP_DIR_1" # # modify the auther-name and auther-email # git filter-repo --force --commit-callback ' # commit.author_name=b"xxx" # commit.author_email=b"xxx@example.com" # ' git remote add origin ssh://"$GERRIT_USER@$IP/$SDK_REPO" 2>/dev/null || \ git remote set-url origin ssh://"$GERRIT_USER@$IP/$SDK_REPO" git push origin --all echo "==== NEW REPO PUSH END ====" 如果不做拆分,传入原始文件为 Deco/base/private/package/webpages M sensitivity public - Deco private module: webpages #BU HN #POC Kester Deng <kester.deng@tp-link.com> #Products Deco Products iplatform/platform/iplatform/stable/private/webpages webpages 怎么推送所有分支。现在只能推送一个master分支
12-13
这是我整个脚本 #!/bin/bash # ================ 参数解析 ================ if [[ $# -ne 8 ]]; then echo "错误:参数数量错误,实际传递参数个数: $# (预期为8)" echo "Usage: $0 <config_file> <gerrit_main_platform> <gerrit_user> <gerrit_ori_host> <gerrit_ori_port> <gerrit_host> <port> <ip>" exit 1 fi # 拆分仓库txt INPUT_FILE="$1" # 原始完整仓库名 GERRIT_MAIN_PLATFORM="$2" # 用户名 GERRIT_USER="$3" # gerrit GERRIT_ORI_HOST="$4" GERRIT_ORI_PORT="$5" # new gerrit GERRIT_HOST="$6" PORT="$7" IP="$8" # 自动推导其他变量 GERRIT_REPO_OWNER="${GERRIT_MAIN_PLATFORM%/*}" # 提取 "Deco" GERRIT_REPO_NAME="${GERRIT_MAIN_PLATFORM##*/}" # 提取 "iplatform" GERRIT_SSH_URL="ssh://${GERRIT_USER}@${GERRIT_ORI_HOST}:${GERRIT_ORI_PORT}/${GERRIT_MAIN_PLATFORM}" # ================ 参数解析结束================ update_gpg_empty_message() { git for-each-ref --format="%(refname:short)" refs/heads/ | while read br; do echo "======== processing branch: $br ========" # 静默切换分支(保留错误处理格式) git checkout "$br" >/dev/null 2>&1 || { echo "== failed to checkout branch $br, skip!" continue } msg="chore: migration complete, adding GPG signature declaration on $(date '+%Y-%m-%d %H:%M:%S') [branch: $br]" # 非交互式提交(完全保留原有输出格式) if GIT_EDITOR=: git commit --allow-empty -s -S --no-edit -m "$msg" >/dev/null 2>&1; then echo "== added GPG signed empty commit on branch '$br'" else echo "== failed to add GPG signed commit on branch '$br'" echo "== hint: check GPG_TTY and GPG key availability" continue fi echo "== processed branch '$br' successfully!" done } echo "git clone gerrit: $GERRIT_SSH_URL" git clone "$GERRIT_SSH_URL" && \ echo "clone Gerrit commit-msg hook..." && \ scp -p -P "$GERRIT_ORI_PORT" "${GERRIT_USER}@${GERRIT_ORI_HOST}:hooks/commit-msg" "${GERRIT_REPO_NAME}/.git/hooks/" cd $GERRIT_REPO_NAME for r in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/); do b=${r#origin/} if ! git show-ref --verify --quiet "refs/heads/$b"; then git branch "$b" "origin/$b" fi done cd .. echo "start split" # 2)拆分ipf和特定机型的sdk,流程类似 # 先克隆一个本地仓库tmp1供拆,并创建对应的远端仓库,该仓库信息需位于txt的末尾 # 然后基于ipf.txt/sdk.txt拆解子仓库:创建远端仓库、克隆本地仓库tmp2、拆解出子仓库并提交至远端、拆解tmp1仓库的对应部分 # 最后提交tmp1仓库远端,该仓库拆解完毕 set -uo pipefail # Remove -e so script does not exit on error sed -i 's/\r$//' "$INPUT_FILE" # Read the whole file into a variable filedata=$(<"$INPUT_FILE") # Split into blocks on blank lines IFS='' blocks=() block="" while IFS= read -r line || [[ -n "$line" ]]; do if [[ -z "$line" ]]; then if [[ -n "$block" ]]; then blocks+=("$block") block="" fi else if [[ -z "$block" ]]; then block="$line" else block="$block"$'\n'"$line" fi fi done <<< "$filedata" # Add last block if missing a trailing blank line if [[ -n "$block" ]]; then blocks+=("$block") fi # the new big repo msg <-> tmp1 OLD_REPO=$(sed -n '$p' <<< "${blocks[-1]}") SDK_REPO=$(sed -n '1p' <<< "${blocks[-1]}") SDK_DESC=$(sed -n '2,5p' <<< "${blocks[-1]}") REPO_DIR=$(sed -n '6p' <<< "${blocks[-1]}") TMP_DIR_1="tmp1" # the new big remote repo echo -e "repo_name: $OLD_REPO" output=$(ssh -p "$PORT" "$GERRIT_USER@$GERRIT_HOST" \ "gerrit create-project \"$SDK_REPO\" --description \"$SDK_DESC\" --submit-type MERGE_IF_NECESSARY" 2>&1) # the new big local repo TMP_DIR_1="tmp1" rm -rf "$TMP_DIR_1" git clone --no-local "$OLD_REPO" "$TMP_DIR_1" >/dev/null # the sub repos splitted echo "==== start delete sub-repos ====" if [[ ${#blocks[@]} -gt 0 ]]; then all_but_last=("${blocks[@]:0:${#blocks[@]}-1}") for block in "${all_but_last[@]}"; do repo=$(sed -n '1p' <<< "$block") desc=$(sed -n '2,5p' <<< "$block") localpath=$(sed -n '6p' <<< "$block") OLD_PATH=$(echo "$localpath" | sed "s#^${REPO_DIR}/##") if [[ -n "$repo" && -n "$desc" ]]; then # build sub repo with desc echo "Creating repository: $repo" echo "==== DESCRIPTION START ====" echo -e "$desc" echo "==== DESCRIPTION END ====" # Temporarily disable exit on error for this command set +e output=$(ssh -p "$PORT" "$GERRIT_USER@$GERRIT_HOST" \ "gerrit create-project \"$repo\" --description \"$desc\" --submit-type MERGE_IF_NECESSARY" 2>&1) status=$? set -e if [[ $status -ne 0 ]]; then if [[ "$output" == *"already exists"* ]]; then echo "?? Repo already exists, skipped: $repo" else echo "?? Failed to create: $repo" echo "$output" fi else echo "? Created: $repo" # split the local repo and pull fi echo "===== SPLIT START =====" # clone the local repo and split to the target sub-repo TMP_DIR_2="tmp2" rm -rf "$TMP_DIR_2" echo "===== SPLIT OLD_REPO:$OLD_REPO OLD_PATH:$OLD_PATH =====" git clone --no-local "$OLD_REPO" "$TMP_DIR_2" >/dev/null cd "$TMP_DIR_2" git filter-repo --subdirectory-filter "$OLD_PATH" echo "==== SPLIT END ====" echo "=== create gpg empty message $OLD_REPO===" update_gpg_empty_message # # modify the auther-name and auther-email # git filter-repo --force --commit-callback ' # commit.author_name=b"xxx" # commit.author_email=b"xxx@example.com" # ' # push the target sub-repo to origin git remote add origin ssh://"$GERRIT_USER@$IP/$repo" 2>/dev/null || \ git remote set-url origin ssh://"$GERRIT_USER@$IP/$repo" git push origin --all cd ../ # delete the sub-repo from the big repo echo "==== REPO delete $repo start ====" cd "$TMP_DIR_1" git filter-repo --path "$OLD_PATH" --invert-paths echo "==== REPO delete $repo end ====" cd ../ fi done else echo "没有找到任何文本块" fi # push the new big repo to origin echo "==== NEW REPO PUSH START ====" cd "$TMP_DIR_1" echo "=== create gpg empty message $OLD_REPO===" update_gpg_empty_message # # modify the auther-name and auther-email # git filter-repo --force --commit-callback ' # commit.author_name=b"xxx" # commit.author_email=b"xxx@example.com" # ' git remote add origin ssh://"$GERRIT_USER@$IP/$SDK_REPO" 2>/dev/null || \ git remote set-url origin ssh://"$GERRIT_USER@$IP/$SDK_REPO" git push origin --all echo "==== NEW REPO PUSH END ====" rm -rf $TMP_DIR_1 rm -rf $TMP_DIR_2
最新发布
12-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值