Git pull etc. do not preserve hardlinks.
I sometimes need hardlinks inside repo, point to somewhere outside, so I wrote these scripts to manage hardlinks.
Last active
May 25, 2019 14:10
-
-
Save zhiyb/28cbf9eb482ac42af97e265ddf57c76a to your computer and use it in GitHub Desktop.
Ancient hard link management script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RELINK_ROOT=/root/Design/relink | |
| RELINK_SUFFIX=.links | |
| VERBOSE=0 | |
| DRYRUN=0 | |
| COLOUR=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| . ~/.relink.rc | |
| colour= | |
| function showHelp | |
| { | |
| echo "Usage: $(basename "$0") [Options] Operations" | |
| echo "Options:" | |
| echo "-v Verbose" | |
| echo "-n Do not actually run commands" | |
| echo "Operations:" | |
| echo "relink" | |
| echo "rebuild" | |
| echo "ln source target" | |
| echo "unlink target" | |
| } | |
| function execute | |
| { | |
| brief="$1" | |
| shift | |
| if ((VERBOSE < 1)); then | |
| if ((COLOUR)); then | |
| ((DRYRUN == 0)) && eval $@ > /dev/null 2>&1 | |
| (($?)) && colour="\e[91m" || colour="\e[92m" | |
| echo -e "$colour $brief\e[0m" | |
| else | |
| echo " $brief" | |
| ((DRYRUN == 0)) && eval $@ | |
| fi | |
| else | |
| echo "$@" | |
| ((DRYRUN == 0)) && eval $@ | |
| fi | |
| } | |
| function loopExecute | |
| { | |
| for listfile in "$RELINK_ROOT"/*"$RELINK_SUFFIX"; do | |
| while read target; do | |
| source="${listfile%.links}" | |
| name="$(basename "$source")" | |
| eval execute $@ | |
| done < $listfile | |
| done | |
| } | |
| function relink | |
| { | |
| loopExecute 'LN\\t$name\\t$target' ln -f '$source' '$target' | |
| } | |
| function rebuild | |
| { | |
| loopExecute 'GEN\\t$name\\t$target' ln -f '$target' '$source'\; break | |
| } | |
| function newFile | |
| { | |
| echo "$RELINK_ROOT/$1" | |
| } | |
| function addTo | |
| { | |
| [ ! -e "$1" ] && echo "List file not exist: $1" >&2 && return 1 | |
| removeFrom $@ | |
| execute "ADD\t$2" echo "$2" ">>" "$1" | |
| } | |
| function removeFrom | |
| { | |
| listfile="$1" | |
| source="${listfile%.links}" | |
| name="$(basename "$source")" | |
| target="$2" | |
| [ "$(cat "$listfile" | grep -Fx "$target")" == "" ] && return 1 | |
| tmp="$(mktemp)" | |
| execute "UNLN\t$name\t$target" cat "$listfile" \| grep -Fxv "$target" \> "$tmp"\; cat "$tmp" \> "$listfile" | |
| rm "$tmp" | |
| } | |
| function absPath | |
| { | |
| readlink -m "$1" | |
| } | |
| function link | |
| { | |
| source="$RELINK_ROOT/$(basename "$1")" | |
| target="$(absPath "$2")" | |
| [ -z "$target" ] && target="$(absPath "$(basename "$1")")" | |
| execute "TOUCH\t$source$RELINK_SUFFIX" touch "$source$RELINK_SUFFIX" | |
| addTo "$source$RELINK_SUFFIX" "$target" | |
| } | |
| function unlink | |
| { | |
| target="$(absPath "$1")" | |
| for listfile in "$RELINK_ROOT"/*"$RELINK_SUFFIX"; do | |
| removeFrom "$listfile" "$target" | |
| done | |
| #execute "RM\t$target" rm "$target" | |
| } | |
| function options | |
| { | |
| case "$1" in | |
| -v ) ((VERBOSE++));; | |
| -n ) DRYRUN=1;; | |
| -nc ) COLOUR=0;; | |
| * ) return 1;; | |
| esac | |
| return 0 | |
| } | |
| while options $@; do | |
| shift | |
| done | |
| op="$1" | |
| shift | |
| case "$op" in | |
| ln ) link $@;; | |
| relink | rebuild | unlink ) $op $@;; | |
| * ) showHelp;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment