From: Oleg Broytman Date: Tue, 29 Apr 2025 12:44:44 +0000 (+0300) Subject: Refactor: Edit `.git/description` as the last step X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;ds=sidebyside;h=refs%2Fheads%2Fmaster;hp=f483adb65ce3845a400488bd289eb3b6eac4b53a;p=git-scripts.git Refactor: Edit `.git/description` as the last step --- diff --git a/README.txt b/README.txt index 3fb6557..24ff47a 100644 --- a/README.txt +++ b/README.txt @@ -2,13 +2,3 @@ git scripts =========== Small scripts I use. Public domain. - -svn/ - scripts for converting a Subversion repository to git using git-svn. -See svn/README.txt. - -clone2current - clone a repository to ~/current/projects; add remote "current" -to the original repository to pull updates from. - -publish2web - clone a repository to ~/Internet/WWW/htdocs/git.phdru.name from -where it will be copied to git.phdru.name web site; add remote "web" -to the original repository to push updates. diff --git a/clone b/clone index 5d1118d..0d2a1fc 100755 --- a/clone +++ b/clone @@ -16,14 +16,13 @@ fi && if [ -d .git ]; then git remote set-url origin "$origin" else - git clone -o origin "$origin" "$local_repo" && - cd "$local_repo" && - chmod a-x .git/config + git clone -o origin "$origin" "$local_repo" fi && "`dirname \"$0\"`"/set-commit-date-recursive && pwd >> "`dirname \"$0\"`"/locate-all.list && -branch="`git rev-parse --abbrev-ref HEAD`" +branch="`git rev-parse --abbrev-ref HEAD`" && git config push.default current && -exec git gc --aggressive +git gc --aggressive && +exec `git var GIT_EDITOR` .git/description diff --git a/copy-scripts/copy-scripts b/copy-scripts/copy-scripts index 2201c0f..83f2d47 100755 --- a/copy-scripts/copy-scripts +++ b/copy-scripts/copy-scripts @@ -12,9 +12,20 @@ remote_host="$1" remote_dir="$2" remote_name="$3" -ssh "$remote_host" "cd $remote_dir && exec git init --bare git-scripts.git" && +ssh "$remote_host" " + cd $remote_dir && + git init --bare git-scripts.git + cd git-scripts.git && + echo Small git scripts >.git/description +" && + git remote add "$remote_name" "$remote_host:$remote_dir/git-scripts.git" && git push "$remote_name" master && -exec ssh "$remote_host" "cd $remote_dir && git clone git-scripts.git && - cd git-scripts.git/hooks && - exec ln -s ../../git-scripts/copy-scripts/post-update" + +exec ssh "$remote_host" " + cd $remote_dir && + git clone git-scripts.git && + cd git-scripts && + echo Small git scripts >.git/description && + cd ../git-scripts.git/hooks && + exec ln -s ../../git-scripts/copy-scripts/post-update" diff --git a/delete b/delete index 6002dc4..1a4bc1e 100755 --- a/delete +++ b/delete @@ -1,6 +1,6 @@ #! /bin/sh -if [ -z "$1" -o -n "$3" ]; then +if [ -z "$1" -o -n "$2" ]; then echo "Usage: $0 /path/to/repo" >&2 exit 1 fi @@ -21,7 +21,7 @@ rm -rf "$path_to_repo" && tmpfile="`mktemp`" && grep -v "^$path_to_repo\$" locate-all.list > "$tmpfile" && -if cmp -s mv "$tmpfile" locate-all.list; then +if cmp -s "$tmpfile" locate-all.list; then exec rm -f "$tmpfile" else exec mv "$tmpfile" locate-all.list diff --git a/filter-branch-all b/filter-branch-all new file mode 100755 index 0000000..6a4abb3 --- /dev/null +++ b/filter-branch-all @@ -0,0 +1,7 @@ +#! /bin/sh + +# Just a reminder how to properly run `git filter-branch --all` + +FILTER_BRANCH_SQUELCH_WARNING=1 exec git filter-branch \ + --tag-name-filter cat "$@" -- --branches --tags + diff --git a/fix-config b/fix-config deleted file mode 100755 index f7d4cda..0000000 --- a/fix-config +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/sh - -g="`git rev-parse --git-dir`" && -exec chmod a-x "$g"/config diff --git a/fix-config-recursive b/fix-config-recursive deleted file mode 100755 index afbe197..0000000 --- a/fix-config-recursive +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/sh - -prog_dir="`dirname \"$0\"`" && -exec "$prog_dir"/run-recursive "$prog_dir"/fix-config diff --git a/fix-config-repos b/fix-config-repos deleted file mode 100755 index da5e46e..0000000 --- a/fix-config-repos +++ /dev/null @@ -1,7 +0,0 @@ -#! /bin/sh - -cd "`dirname \"$0\"`" && -prog_dir="`pwd`" && -export prog_dir - -exec "$prog_dir"/run-repos "$prog_dir"/fix-config-recursive diff --git a/fix-permissions-repos b/fix-permissions-repos new file mode 100755 index 0000000..b8e01ca --- /dev/null +++ b/fix-permissions-repos @@ -0,0 +1,15 @@ +#! /bin/sh + +prog_dir="`dirname \"$0\"`" && + +cat "$prog_dir"/locate-all.list | + grep -v "^$HOME/Internet/WWW/htdocs/git.phdru.name/" | +while read d; do + echo "----- $d -----"; cd "$d" && + if test -d "$d/.git"; then cd "$d/.git"; fi && # bare repo + chmod -R u=rwX,go= . && + find objects -type f -exec chmod u=r,go= '{}' \+ +done && + +cd "$HOME/Internet/WWW/htdocs/git.phdru.name/" && +pwd && exec chmod -R u=rwX,go=rX . diff --git a/fork b/fork index 5188356..6c07d6c 100755 --- a/fork +++ b/fork @@ -15,12 +15,13 @@ if [ -d "$local_repo"/.git ]; then fi && if [ -d .git ]; then - git remote add upstream "$upstream" - git remote set-url origin "$origin" || git remote add origin "$origin" + git remote add upstream "$upstream" || + git remote set-url upstream "$upstream" + git remote set-url origin "$origin" || + git remote add origin "$origin" else git clone -o upstream "$upstream" "$local_repo" && cd "$local_repo" && - chmod a-x .git/config && git remote add origin "$origin" fi && @@ -37,4 +38,5 @@ git config branch.$branch.remotepush origin && git config branch.$branch.merge refs/heads/$branch && git config push.default current && -exec git gc --aggressive +git gc --aggressive && +exec `git var GIT_EDITOR` .git/description diff --git a/git-current-branch b/git-current-branch index 19cedf4..d485dea 100755 --- a/git-current-branch +++ b/git-current-branch @@ -4,6 +4,7 @@ git branch --show-current git describe --all +git for-each-ref --contains=HEAD --format="%(refname)" refs/heads/ git name-rev --name-only HEAD git rev-parse --abbrev-ref HEAD diff --git a/hooks/post-checkout/set-last-commit-date-all b/hooks/post-checkout/set-last-commit-date-all index 1c87fba..4cceb0f 100755 --- a/hooks/post-checkout/set-last-commit-date-all +++ b/hooks/post-checkout/set-last-commit-date-all @@ -8,7 +8,7 @@ new_HEAD="$2" new_branch="$3" if [ "$new_branch" = 1 ]; then - commit_date="`git show --format='%cD' -s`" && + commit_date="`git show --pretty=tformat:'%cD' -s`" && find . \( -name .git -type d -prune \) -o \ -exec touch --date="$commit_date" '{}' \+ fi diff --git a/hooks/post-checkout/set-last-commit-date-changed b/hooks/post-checkout/set-last-commit-date-changed index 1a6b0ac..267711e 100755 --- a/hooks/post-checkout/set-last-commit-date-changed +++ b/hooks/post-checkout/set-last-commit-date-changed @@ -8,7 +8,7 @@ new_HEAD="$2" new_branch="$3" if [ "$new_branch" = 1 ]; then - commit_date="`git show --format='%cD' -s`" && + commit_date="`git show --pretty=tformat:'%cD' -s`" && touch --date="$commit_date" `git diff --name-only HEAD~` fi diff --git a/hooks/post-checkout/set-last-commit-date-committed b/hooks/post-checkout/set-last-commit-date-committed index 1e88974..f55ea77 100755 --- a/hooks/post-checkout/set-last-commit-date-committed +++ b/hooks/post-checkout/set-last-commit-date-committed @@ -8,7 +8,7 @@ new_HEAD="$2" new_branch="$3" if [ "$new_branch" = 1 ]; then - commit_date="`git show --format='%cD' -s`" && + commit_date="`git show --pretty=tformat:'%cD' -s`" && touch --date="$commit_date" `git ls-files --cached` fi diff --git a/init b/init new file mode 100755 index 0000000..849ba89 --- /dev/null +++ b/init @@ -0,0 +1,17 @@ +#! /bin/sh + +if [ -n "$2" ]; then + echo "Usage: $0 [subdir]" >&2 + exit 1 +fi + +set -e +subdir="$1" + +if [ -n "$subdir" ]; then + mkdir -p "$subdir" + cd "$subdir" +fi + +git init +pwd >> "`dirname \"$0\"`"/locate-all.list diff --git a/pull-usrlocalsrc b/pull-usrlocalsrc index c7159ad..4b524d7 100755 --- a/pull-usrlocalsrc +++ b/pull-usrlocalsrc @@ -6,7 +6,7 @@ has_remote() { prog_dir="`dirname \"$0\"`" && -cat "$prog_dir"/locate-all.list | grep -F /usr/local/src | +cat "$prog_dir"/locate-all.list | grep "^/usr/local/src/" | while read d; do echo "----- $d -----"; cd "$d" && if has_remote upstream && has_remote origin; then diff --git a/set-commit-date b/set-commit-date index fcc7539..db48c59 100755 --- a/set-commit-date +++ b/set-commit-date @@ -4,5 +4,5 @@ # and set the file's modification time to that date/time. git ls-tree -r --name-only HEAD | while read filename; do - touch --date="`git log -1 --format=\"%cD\" -- \"$filename\"`" "$filename" + touch --date="`git log -1 --pretty=tformat:\"%cD\" -- \"$filename\"`" "$filename" done diff --git a/set-commit-date.py b/set-commit-date.py index c0aaa8c..67d2c4f 100755 --- a/set-commit-date.py +++ b/set-commit-date.py @@ -34,7 +34,8 @@ separator = '----- GIT LOG SEPARATOR -----' git_log = subprocess.Popen(['git', 'log', '-m', '--first-parent', '--name-status', '--no-color', - '--format=%s%%n%%%st' % (separator, date_format)], + '--pretty=tformat:%s%%n%%%st' % ( + separator, date_format)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) changed_files = set() diff --git a/submodules/remove b/submodules/remove index 1db57c2..d66ef34 100755 --- a/submodules/remove +++ b/submodules/remove @@ -1,8 +1,12 @@ #! /bin/sh # See https://stackoverflow.com/q/1260748/7976758 +case "$1" in + -c|--cached) cached=--cached; shift ;; +esac && + if [ $# != 1 ]; then - echo "Usage: $0 submodule_name" >&2 + echo "Usage: $0 [-c|--cached] submodule_name" >&2 exit 1 fi @@ -14,21 +18,22 @@ if [ \! -f .gitmodules ]; then fi name="$1" -path="$(git config -f .gitmodules --get "submodule.$name.path")" +path="`git config -f .gitmodules --get "submodule.$name.path"`" if [ -z "$path" ]; then echo "Error: submodule $name does not exist" >&2 exit 1 fi -git rm "$path" && -rm -rf "`git rev-parse --git-dir`"/modules/"$name" && +git rm $cached "$path" && +GIT_DIR=`git rev-parse --git-dir` && +rm -rf "$GIT_DIR"/modules/"$name" && git config --remove-section submodule."$name" && if [ \! -s .gitmodules ]; then git rm -f .gitmodules fi && -if [ -z $(ls -A "`git rev-parse --git-dir`/modules") ]; then - exec rmdir "`git rev-parse --git-dir`/modules" +if [ -z "`ls -A \"$GIT_DIR/modules\"`" ]; then + exec rmdir "$GIT_DIR/modules" fi diff --git a/update-remotes b/update-remotes index dca6f3f..5237db9 100755 --- a/update-remotes +++ b/update-remotes @@ -1,31 +1,40 @@ #! /bin/sh +set -e -current="`git config --get --path remote.current.url`" -origin="`git config --get --path remote.origin.url`" -branches="master ${1:+$@}" +current="`git config --get --path remote.current.url`" || : +origin="`git config --get --path remote.origin.url`" || : +web="`git config --get --path remote.web.url`" || : +remotes="`git config --get update-remotes.remotes`" || : +branches="`git config --get-all update-remotes.branches`" || : +files="`git config --get-all update-remotes.files`" || : if [ -n "$origin" ]; then - if [ -n "$current" ]; then - echo "Config error (both origin and current)" >&2 - exit 1 - else - cd "$origin" && - exec ./update-remotes - fi + if [ -n "$current" ]; then + echo "Config error (both origin and current)" >&2 + exit 1 + else + rsync -ahPv $files update-remotes "$origin" + cd "$origin" + exec ./update-remotes + fi elif [ -z "$current" ]; then - echo "Wrong directory error (neither origin nor current)" >&2 - exit 1 + echo "Wrong directory error (neither origin nor current)" >&2 + exit 1 fi -git fetch current && -{ git pull --ff-only current master || git reset --hard current/master ; } && -git set-date && +git fetch current master $branches +{ git pull --ff-only current master || git reset --hard current/master ; } +git set-date -web="`git config --get --path remote.web.url`" && if [ -n "$web" ]; then - git push --force web $branches -fi && + git push --force web master $branches +fi + +cd "$current" + +for remote in $remotes; do + git push --force $remote master $branches +done -cd "$current" && -git pull origin && +git pull origin exec git set-date