Advertisement
Ed94

Simple update git repo script

Jan 9th, 2025
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.37 KB | Source Code | 0 0
  1. update_git_repo() {
  2.     local path="$1"
  3.     local url="$2"
  4.     local build_command="$3"
  5.  
  6.     if [ -z "$build_command" ]; then
  7.         echo "Attempted to call update_git_repo without build_command specified"
  8.         return
  9.     fi
  10.  
  11.     local repo_name=$(basename "$url" .git)
  12.  
  13.     local last_built_commit="$path_build/last_built_commit_$repo_name.txt"
  14.     if [ ! -d "$path" ]; then
  15.         echo "Cloning repo from $url to $path"
  16.         git clone "$url" "$path"
  17.  
  18.         echo "Building $url"
  19.         pushd "$path" > /dev/null
  20.         chmod +x "$build_command"
  21.         eval "$build_command"
  22.         popd > /dev/null
  23.  
  24.         git -C "$path" rev-parse HEAD > "$last_built_commit"
  25.         binaries_dirty=true
  26.         echo
  27.         return
  28.     fi
  29.  
  30.     git -C "$path" fetch
  31.     local latest_commit_hash=$(git -C "$path" rev-parse '@{u}')
  32.     local last_built_hash=""
  33.     [ -f "$last_built_commit" ] && last_built_hash=$(cat "$last_built_commit")
  34.  
  35.     if [ "$latest_commit_hash" = "$last_built_hash" ]; then
  36.         echo
  37.         return
  38.     fi
  39.  
  40.     echo "Build out of date for: $path, updating"
  41.     echo 'Pulling...'
  42.     git -C "$path" pull
  43.  
  44.     echo "Building $url"
  45.     pushd "$path" > /dev/null
  46.     chmod +x "$build_command"
  47.     eval "$build_command"
  48.     popd > /dev/null
  49.  
  50.     echo "$latest_commit_hash" > "$last_built_commit"
  51.     binaries_dirty=true
  52.     echo
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement