HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/local/bin/dhwp/dhwp/core/git.py
import re
import sys
import os
from cement import shell
from cement.utils.misc import minimal_logger
LOG = minimal_logger(__name__)


class Git:

    def __init__(self, path):
        self.path = path
        self.git_path = path + '/.git'
        shell.cmd('cd %s && git config --global user.email "robot@dreamhost.com"' % self.path)
        shell.cmd('cd %s && git config --global user.name "Robot"' % self.path)

    def cmd(self, cmd, args=''):
        out, err, code = shell.cmd('which git')

        if code > 0:
            LOG.fatal("cannot find git, please install it first")
            sys.exit(1)

        if not os.path.isdir(self.path):
            LOG.fatal("ERROR: %s is not a directory" % self.path)
            sys.exit(1)

        # TODO put this somewhere else

        command = "cd %s && git --git-dir=%s %s %s" % (self.path, self.git_path, cmd, args)

        LOG.info("git command : %s" % command)
        out, err, code = shell.cmd(command)
        out = out.decode("utf-8")
        err = err.decode("utf-8")
        LOG.info("git output : %s %s" % (out, err))

        if code > 0:
            LOG.fatal("GIT failed with error %s" % err)
            sys.exit(1)

        return out

    def commit(self, message='', exclude_media=False, exclude_live_archives=False):
        git_ignore_file = self.path + '/.gitignore'
        self.git_ignore(exclude_media, exclude_live_archives)

        self.cmd('add', '.')

        commit_args = '-a -m \"%s\"' % message
        if self.has_changes():
            self.cmd('commit', commit_args)

        if os.path.isfile(git_ignore_file):
            os.unlink(git_ignore_file)

    def git_ignore(self, exclude_media=False, exclude_live_archives=False):
        git_ignore_file = self.path + '/.gitignore'
        with open(git_ignore_file, 'w') as gi:
            gi.write("/.gitignore\nwp-content/cache\nwp-content/upgrade\nwp-config.php\n/.htaccess\n.maintenance\nwp-content/object-cache.php\n")
            if exclude_media:
                gi.write("wp-content/uploads/*\nwp-content/gallery/*\n")

            if exclude_live_archives:
                gi.write("wp-content/updraft\n**/*.sql\nwp-content/backups\n"
                         "wp-snapshot\nwp-content/ai1wm-backups\nwp-content/**/*.zip\n"
                         "wp-content/**/*.gz\nwp-content/**/*.tar\n")

    def has_changes(self):
        result = self.cmd('status')
        if 'nothing to commit' in result:
            return 0
        else:
            return 1

    def wipe(self):
        git_ignore_file = self.path + '/.gitignore'
        if os.path.isfile(git_ignore_file):
            os.unlink(git_ignore_file)

        if os.path.isdir(self.git_path):
            shell.cmd("git --git-dir=%s worktree prune" % self.git_path)
            shell.cmd("rm -rf " + self.git_path)