[sysadmin-bin] Rework git hooks to support GitLab hashed storage



commit d4b442bdb2bd43fa2221623c1065a4f74d18e597
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Fri Jul 10 09:34:33 2020 +0200

    Rework git hooks to support GitLab hashed storage

 git/git.py                          | 21 +++++-------------
 git/gnome-post-receive              | 20 +++++++++++------
 git/gnome-post-receive-email        | 44 ++++++++++++++++++-------------------
 git/gnome-pre-receive               | 41 ++++++++++++++++++----------------
 git/log-push                        |  3 +--
 git/post-receive-notify-kgb         | 11 ++--------
 git/post-receive-update-description |  6 +----
 git/pre-receive-check-maintainers   | 14 ++++--------
 git/pre-receive-check-po            |  2 --
 git/pre-receive-check-policy        | 14 +++---------
 10 files changed, 74 insertions(+), 102 deletions(-)
---
diff --git a/git/git.py b/git/git.py
index ddf1380..3c7d3bd 100644
--- a/git/git.py
+++ b/git/git.py
@@ -176,26 +176,17 @@ def commit_oneline(commit):
 # Return the directory name with .git stripped as a short identifier
 # for the module
 def get_module_name():
-    try:
-        git_dir = git.rev_parse(git_dir=True, _quiet=True)
-    except CalledProcessError:
-        die("GIT_DIR not set")
+    gl_project_path = os.getenv("GL_PROJECT_PATH")
+    if not gl_project_path:
+        die("GL_PROJECT_PATH not set")
 
-    # Use the directory name with .git stripped as a short identifier
-    absdir = os.path.abspath(git_dir)
-    if absdir.endswith(os.sep + '.git'):
-        absdir = os.path.dirname(absdir)
-    projectshort = os.path.basename(absdir)
-    if projectshort.endswith(".git"):
-        projectshort = projectshort[:-4]
+    return gl_project_path.split("/")[-1]
 
-    return projectshort
 
 # Return the project description or '' if it is 'Unnamed repository;'
 def get_project_description():
-    try:
-        git_dir = git.rev_parse(git_dir=True, _quiet=True)
-    except CalledProcessError:
+    git_dir = os.getenv("GIT_DIR")
+    if not git_dir:
         die("GIT_DIR not set")
 
     projectdesc = ''
diff --git a/git/gnome-post-receive b/git/gnome-post-receive
index b81f154..4e42943 100755
--- a/git/gnome-post-receive
+++ b/git/gnome-post-receive
@@ -15,16 +15,22 @@
 
 BINDIR=/home/admin/bin/git
 
+if [[ -v GL_PROJECT_PATH ]]; then
+  export NAMESPACE="${GL_PROJECT_PATH%/*}"
+  export REPONAME="${GL_PROJECT_PATH##*/}"
+  export GIT_DIR="$PWD"
+  export GL_USERNAME
+else
+  export NAMESPACE=nongitlab
+  _GIT_DIR="$(git rev-parse --git-dir 2>/dev/null)"
+  export GIT_DIR="$(cd $_GIT_DIR && pwd)"
+  export REPONAME="$(basename ${GIT_DIR%.git})"
+fi
+
 # If the committing user has a homedir with a .gitconfig in it, it we
 # don't want that to affect our operation. (Should this just be handled
 # in run-git-or-special-cmd?)
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
-GIT_CONFIG="${GIT_DIR}/config"
-export GIT_CONFIG
-
-if [ $(hostname) == 'gitlab.gnome.org' ]; then
-    export GL_USERNAME
-fi
+export GIT_CONFIG="${GIT_DIR}/config"
 
 tee >($BINDIR/log-push 1>&2) \
     >($BINDIR/post-receive-update-description 1>&2) \
diff --git a/git/gnome-post-receive-email b/git/gnome-post-receive-email
index f0066b9..829f12c 100755
--- a/git/gnome-post-receive-email
+++ b/git/gnome-post-receive-email
@@ -956,39 +956,39 @@ def main():
         sys.exit(1)
 
     if socket.gethostname() == 'gitlab.gnome.org':
-       user = os.environ['GL_USERNAME']
+        user = os.environ['GL_USERNAME']
         user = gl.users.list(username='%s' % user)
-       user = user[0]
+        user = user[0]
 
         if len(user.attributes['identities']) > 0:
-           for index, _ in enumerate(user.attributes['identities']):
-               provider = user.attributes['identities'][index]['provider']
-               if provider == 'ldapmain':
-                   username = 
user.attributes['identities'][index]['extern_uid'].split(',')[0].replace('uid=', '')
+            for index, _ in enumerate(user.attributes['identities']):
+                provider = user.attributes['identities'][index]['provider']
+                if provider == 'ldapmain':
+                    username = 
user.attributes['identities'][index]['extern_uid'].split(',')[0].replace('uid=', '')
 
-                   user_fullname = user.attributes['name']
-                   user_fullname = user_fullname.encode('utf-8')
+                    user_fullname = user.attributes['name']
+                    user_fullname = user_fullname.encode('utf-8')
 
-                   user_entry = username
+                    user_entry = username
 
         gitlab_name  = user.attributes['name']
         gitlab_email = user.attributes['email']
     else:
         # Figure out a human-readable username
         try:
-           entry = pwd.getpwuid(os.getuid())
-           gecos = entry.pw_gecos
-       except:
-           gecos = None
-
-       if gecos != None:
-           # Typical GNOME account have John Doe <john doe example com> for the GECOS.
-           # Comma-separated fields are also possible
-           m = re.match("([^,<]+)", gecos)
-           if m:
-               fullname = m.group(1).strip()
-               if fullname != "":
-                   user_fullname = fullname
+            entry = pwd.getpwuid(os.getuid())
+            gecos = entry.pw_gecos
+        except:
+            gecos = None
+
+        if gecos != None:
+            # Typical GNOME account have John Doe <john doe example com> for the GECOS.
+            # Comma-separated fields are also possible
+            m = re.match("([^,<]+)", gecos)
+            if m:
+                fullname = m.group(1).strip()
+                if fullname != "":
+                    user_fullname = fullname
 
         user_entry = os.environ['USER']
 
diff --git a/git/gnome-pre-receive b/git/gnome-pre-receive
index 3e6458b..c418987 100755
--- a/git/gnome-pre-receive
+++ b/git/gnome-pre-receive
@@ -7,30 +7,33 @@
 # success or failure of the update.
 
 BINDIR=/home/admin/bin/git
-GITLABDIR=/var/opt/gitlab/git-data/repositories
+
+if [[ -v GL_PROJECT_PATH ]]; then
+  export NAMESPACE="${GL_PROJECT_PATH%/*}"
+  export REPONAME="${GL_PROJECT_PATH##*/}"
+  export GIT_DIR="$PWD"
+  export GL_USERNAME
+else
+  export NAMESPACE=nongitlab
+  _GIT_DIR="$(git rev-parse --git-dir 2>/dev/null)"
+  export GIT_DIR="$(cd $_GIT_DIR && pwd)"
+  export REPONAME="$(basename ${GIT_DIR%.git})"
+fi
 
 # If the committing user has a homedir with a .gitconfig in it, it we
 # don't want that to affect our operation. (Should this just be handled
 # in run-git-or-special-cmd?)
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
 GIT_CONFIG="${GIT_DIR}/config"
 export GIT_CONFIG
 
-# Use the directory name with .git stripped as a short identifier
-absdir=$(cd $GIT_DIR && pwd)
-basedir=$(dirname $absdir)
-projectshort=$(basename ${absdir%.git})
-
-if [ $(hostname) == 'gitlab.gnome.org' ]; then
-  if [[ $basedir = "${GITLABDIR}/GNOME" || $basedir = "${GITLABDIR}/Infrastructure" ]]; then
-    git config hooks.emailprefix 1>/dev/null
-    if [ "$?" -eq 1 ]; then
-      git config hooks.emailprefix ""
-    fi
-    git config hooks.mailinglist 1>/dev/null
-    if [ "$?" -eq 1 ]; then
-      git config hooks.mailinglist commits-list gnome org
-    fi
+if [[ $NAMESPACE = "GNOME" || $NAMESPACE = "Infrastructure" ]]; then
+  git config hooks.emailprefix 1>/dev/null
+  if [ "$?" -eq 1 ]; then
+    git config hooks.emailprefix ""
+  fi
+  git config hooks.mailinglist 1>/dev/null
+  if [ "$?" -eq 1 ]; then
+    git config hooks.mailinglist commits-list gnome org
   fi
 fi
 
@@ -48,8 +51,8 @@ while read oldrev newrev refname; do
     # to bypass if l10.gnome.org were compromised.
     [ $my_uid != $translations_uid ] || $BINDIR/pre-receive-check-translations $oldrev $newrev $refname || 
exit 1
 
-    if [[ $basedir = "${GITLABDIR}/GNOME" || $basedir = "${GITLABDIR}/Infrastructure" ]]; then
-        if [[ ! "${projectshort}.git" =~ .wiki.git$ ]]; then
+    if [[ $NAMESPACE = "GNOME" || $NAMESPACE = "Infrastructure" ]]; then
+        if [[ ! "$GIT_DIR" =~ .wiki.git$ ]]; then
             $BINDIR/pre-receive-check-policy $oldrev $newrev $refname || exit 1
             $BINDIR/pre-receive-check-maintainers $oldrev $newrev $refname || exit 1
             $BINDIR/pre-receive-check-po $oldrev $newrev $refname || exit 1
diff --git a/git/log-push b/git/log-push
index b373a85..a96bb90 100755
--- a/git/log-push
+++ b/git/log-push
@@ -5,8 +5,6 @@
 #
 #  old_revision_hash new_revision_hash refname
 
-# Set GIT_DIR either from the working directory, or from the environment variable.
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
 if [ -z "$GIT_DIR" ]; then
      echo >&2 "fatal: post-receive: GIT_DIR not set"
      exit 1
@@ -19,6 +17,7 @@ if [ $(hostname) == 'gitlab.gnome.org' ]; then
 else
     user=$(whoami)
 fi
+
 # RFC 822 date with a fixed UTC timezone (Thu, 05 Feb 2009 19:25:44 +0000)
 date=$(TZ=UTC date -R)
 
diff --git a/git/post-receive-notify-kgb b/git/post-receive-notify-kgb
index afc3ca9..b682479 100755
--- a/git/post-receive-notify-kgb
+++ b/git/post-receive-notify-kgb
@@ -2,17 +2,10 @@
 # actually match the repository-name you are going to configure on kgb.conf and kgb-client.conf. More
 # details at https://wiki.gnome.org/Sysadmin/IRC#Other_details:_Commits_Bot.
 
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
-ABSDIR=$(cd $GIT_DIR && pwd)
-BASEDIR=$(dirname $ABSDIR)
-REPOSITORY_ID=$(basename "$PWD")
-REPOSITORY_ID=${REPOSITORY_ID%.git}
-
 # Get the repo's mainline branch name
 mainline=$(git symbolic-ref --quiet --short HEAD)
 mainline=${mainline:-"master"}
 
-if [[ $BASEDIR = '/var/opt/gitlab/git-data/repositories/GNOME' || $BASEDIR = 
'/var/opt/gitlab/git-data/repositories/Infrastructure' ]]; then
-      export GIT_DIR
-      egrep "refs/heads/($mainline|gtk\-|glib\-|gnome\-)" | kgb-client --conf 
/home/admin/KGB/kgb-client-$REPOSITORY_ID.conf --repository git --git-reflog - > /dev/null 2>&1
+if [[ $NAMESPACE = 'GNOME' || $NAMESPACE = 'Infrastructure' ]]; then
+      egrep "refs/heads/($mainline|gtk\-|glib\-|gnome\-)" | kgb-client --conf 
/home/admin/KGB/kgb-client-$REPONAME.conf --repository git --git-reflog - > /dev/null 2>&1
 fi
diff --git a/git/post-receive-update-description b/git/post-receive-update-description
index ffa7748..8c774a8 100755
--- a/git/post-receive-update-description
+++ b/git/post-receive-update-description
@@ -4,11 +4,7 @@ umask 0002
 
 BINDIR=/home/admin/bin/git
 
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
-
-# Use the directory name with .git stripped as a short identifier
-absdir=$(cd $GIT_DIR && pwd)
-projectshort=$(basename ${absdir%.git})
+projectshort=$REPONAME
 
 # Get the repo's mainline branch name
 mainline=$(git symbolic-ref --quiet --short HEAD)
diff --git a/git/pre-receive-check-maintainers b/git/pre-receive-check-maintainers
index 9c243ec..100f7d1 100755
--- a/git/pre-receive-check-maintainers
+++ b/git/pre-receive-check-maintainers
@@ -6,12 +6,6 @@
 
 BINDIR=/home/admin/bin/git
 
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
-
-# Use the directory name with .git stripped as a short identifier
-absdir=$(cd $GIT_DIR && pwd)
-projectshort=$(basename ${absdir%.git})
-
 # Get the repo's mainline branch name
 mainline=$(git symbolic-ref --quiet --short HEAD)
 mainline=${mainline:-"master"}
@@ -47,17 +41,17 @@ check_maintainers() {
        return 0
     fi
 
-    if git cat-file -e $newrev:$projectshort.doap 2>/dev/null ; then
+    if git cat-file -e $newrev:$REPONAME.doap 2>/dev/null ; then
        # There's a DOAP file. For performance reasons and to allow having fairly
        # strict validation without being annoying, we only validate the DOAP file
         # if it changed
-       if git diff-tree --name-only -r $oldrev $newrev | grep -q $projectshort.doap ; then
-           if ! git cat-file blob $newrev:$projectshort.doap | $BINDIR/validate-doap $projectshort ; then
+       if git diff-tree --name-only -r $oldrev $newrev | grep -q $REPONAME.doap ; then
+           if ! git cat-file blob $newrev:$REPONAME.doap | $BINDIR/validate-doap $REPONAME ; then
                return 1
            fi
        fi
     else
-        echo "A valid $projectshort.doap file is required. See https://wiki.gnome.org/Git/FAQ"; >&2
+        echo "A valid $REPONAME.doap file is required. See https://wiki.gnome.org/Git/FAQ"; >&2
         return 1
     fi
 }
diff --git a/git/pre-receive-check-po b/git/pre-receive-check-po
index a248121..20b3ea9 100755
--- a/git/pre-receive-check-po
+++ b/git/pre-receive-check-po
@@ -154,8 +154,6 @@ check_pos() {
 # Use a newer version of the gettext tools than the ones shipped with RHEL 5
 PATH=/usr/libexec/gettext17:$PATH
 
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
-
 # Don't check for .po files when check_po is set to 
 # false on the repository's config file 
 check_po=$(git config hooks.checkpo 2>/dev/null || echo true)
diff --git a/git/pre-receive-check-policy b/git/pre-receive-check-policy
index ad379fb..b73fd09 100755
--- a/git/pre-receive-check-policy
+++ b/git/pre-receive-check-policy
@@ -12,8 +12,6 @@
 # Used in some of the messages
 server=gitlab.gnome.org
 
-GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
-
 # Get the repo's mainline branch name
 mainline=$(git symbolic-ref --quiet --short HEAD)
 mainline=${mainline:-"master"}
@@ -162,15 +160,9 @@ check_ref_update() {
                         # our repositories anyways, but catching it here would
                         # allow overriding without having to change the config
                         # temporarily.
-                        if [ "`hostname`" == 'git.gnome.org' ]; then
-                            if ! $is_wip && ! forced; then
-                                nonfastforward=true
-                            fi
-                        elif [ "`hostname`" == 'gitlab.gnome.org' ]; then
-                            if [[ "$is_protected" = true ]]; then
-                                nonfastforward=true
-                            fi
-                        fi
+                       if [[ "$is_protected" = true ]]; then
+                           nonfastforward=true
+                       fi
                     fi
 
                     if [ "$nonfastforward" = true ] ; then


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]