Debian Codegraph @

.gitlab/scripts/git_try_to_push

raw
1 #!/bin/bash
2
3 # Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
4 # at the Lawrence Livermore National Laboratory. All Rights reserved. See files
5 # LICENSE and NOTICE for details. LLNL-CODE-806117.
6 #
7 # This file is part of the MFEM library. For more information and source code
8 # availability visit https://mfem.org.
9 #
10 # MFEM is free software; you can redistribute it and/or modify it under the
11 # terms of the BSD-3 license. We welcome feedback and contributions, see file
12 # CONTRIBUTING.md for details.
13
14 # Try to push to the remote 5 times. If the push fails, and the local and remote
15 # have diverged, then pull from the remote to merge changes, and try pushing
16 # again. If some other failure happens
17 for i in {1..5}; do
18 git push origin master && exit 0
19 # Wait for 20 seconds in case someone else is pushing to the remote
20 # concurrently
21 sleep 20
22 # Fetch any updates from the remote
23 git remote update
24 # Get the latest commit on the local branch
25 LOCAL=$(git rev-parse @)
26 # Get the latest commit on the remote
27 REMOTE=$(git rev-parse @{u})
28 # Get the common ancestor
29 BASE=$(git merge-base @ @{u})
30 # Have the local and remote diverged?
31 if [[ $LOCAL != $REMOTE && $LOCAL != $BASE && $REMOTE != $BASE ]]; then
32 git pull
33 if [[ $? == 0 ]]; then
34 continue
35 else
36 exit 1 # Something else went wrong trying to pull
37 fi
38 fi
39 done
40
41 exit 1 # Did not succeed in 5 attempts