Updating git -> git_integration, splitting git command into git/git evennia, adding git evennia tests

This commit is contained in:
Wendy Wang 2022-10-07 10:41:30 +02:00
parent 4d209b12b0
commit 4454eb7fce
4 changed files with 98 additions and 48 deletions

View file

@ -2,7 +2,7 @@
Contribution by helpme (2022)
A module to integrate a stripped-down version of git within the game, allowing developers to view their git status, change branches, and pull updated code of both their local game and evennia core. After a successful pull or checkout, the git command will reload the game: Manual restarts may be required to to apply certain changes that would impact persistent scripts etc.
A module to integrate a stripped-down version of git within the game, allowing developers to view their git status, change branches, and pull updated code of both their local mygame repo and Evennia core. After a successful pull or checkout, the git command will reload the game: Manual restarts may be required to to apply certain changes that would impact persistent scripts etc.
Once the contrib is set up, integrating remote changes is as simple as entering the following into your game:
@ -10,7 +10,7 @@ Once the contrib is set up, integrating remote changes is as simple as entering
git pull
```
Of course, your game directory must be a git directory to begin with for this command to function. [Get started with version control here.](https://www.evennia.com/docs/1.0-dev/Coding/Version-Control.html)
The repositories you want to work with, be it only your local mygame repo, only Evennia core, or both, must be git directories for the command to function. If you are only interested in using this to get upstream Evennia changes, only the Evennia repository needs to be a git repository. [Get started with version control here.](https://www.evennia.com/docs/1.0-dev/Coding/Version-Control.html)
## Dependencies
@ -42,7 +42,7 @@ Then `reload` to make the git command available.
## Usage
This utility will only work if your game and evennia directories are git directories. If they are not, you will be prompted to initiate your directory as a git repository using the following commands in your terminal:
This utility will only work if the directory you wish to work with is a git directory. If they are not, you will be prompted to initiate your directory as a git repository using the following commands in your terminal:
```
git init
@ -57,11 +57,11 @@ The supported commands are:
* git checkout 'branch': Checkout a branch.
* git pull: Pull the latest code from your current branch.
* All of these commands are also available with 'evennia', to serve the same functionality related to your evennia directory. So:
* git status evennia
* git branch evennia
* git checkout evennia 'branch'
* git pull evennia: Pull the latest evennia code.
* All of these commands are also available with 'evennia', to serve the same functionality related to your Evennia directory. So:
* git evennia status
* git evennia branch
* git evennia checkout 'branch'
* git evennia pull: Pull the latest Evennia code.
## Settings Used

View file

@ -7,34 +7,17 @@ from evennia.server.sessionhandler import SESSIONS
import git
import datetime
class CmdGit(MuxCommand):
class GitCommand(MuxCommand):
"""
Pull the latest code from your repository or checkout a different branch.
Usage:
git status - View an overview of your git repository.
git branch - View available branches.
git checkout main - Checkout the main branch of your code.
git pull - Pull the latest code from your current branch.
For updating evennia code, the same commands are available with 'git evennia':
git evennia status
git evennia branch
git evennia checkout <branch>
git evennia pull
If there are any conflicts encountered, the command will abort. The command will reload your game after pulling new code automatically, but for changes involving persistent scripts etc, you may need to manually restart.
The shared functionality between git/git evennia
"""
key = "@git"
aliases = ["@git evennia"]
locks = "cmd:pperm(Developer)"
help_category = "System"
def parse(self):
"""
Parse the arguments and ensure git repositories exist. Fail with InterruptCommand if git repositories not found.
Parse the arguments, set default arg to 'status' and check for existence of currently targeted repo
"""
super().parse()
if self.args:
split_args = self.args.strip().split(" ", 1)
self.action = split_args[0]
@ -45,26 +28,17 @@ class CmdGit(MuxCommand):
else:
self.action = "status"
self.args = ""
err_msgs = ["|rInvalid Git Repository|n:",
self.err_msgs = ["|rInvalid Git Repository|n:",
"The {repo_type} repository is not recognized as a git directory.",
"In order to initialize it as a git directory, you will need to access your terminal and run the following commands from within your directory:",
" git init",
" git remote add origin {remote_link}"]
if self.cmdstring == "git evennia":
directory = settings.EVENNIA_DIR
repo_type = "Evennia"
remote_link = "https://github.com/evennia/evennia.git"
else:
directory = settings.GAME_DIR
repo_type = "game"
remote_link = "[your remote link]"
try:
self.repo = git.Repo(directory, search_parent_directories=True)
self.repo = git.Repo(self.directory, search_parent_directories=True)
except git.exc.InvalidGitRepositoryError:
err_msg = '\n'.join(err_msgs).format(repo_type=repo_type, remote_link=remote_link)
err_msg = '\n'.join(self.err_msgs).format(repo_type=self.repo_type, remote_link=self.remote_link)
self.caller.msg(err_msg)
raise InterruptCommand
@ -160,6 +134,51 @@ class CmdGit(MuxCommand):
caller.msg("You can only git status, git branch, git checkout, or git pull.")
return
class CmdGitEvennia(GitCommand):
"""
Pull the latest code from the evennia core or checkout a different branch.
Usage:
git evennia status - View an overview of the evennia repository status.
git evennia branch - View available branches in evennia.
git evennia checkout <branch> - Checkout a different branch in evennia.
git evennia pull - Pull the latest evennia code.
For updating your local mygame repository, the same commands are available with 'git'.
If there are any conflicts encountered, the command will abort. The command will reload your game after pulling new code automatically, but for some changes involving persistent scripts etc, you may need to manually restart.
"""
key = "git evennia"
locks = "cmd:pperm(Developer)"
help_category = "System"
directory = settings.EVENNIA_DIR
repo_type = "Evennia"
remote_link = "https://github.com/evennia/evennia.git"
class CmdGit(GitCommand):
"""
Pull the latest code from your repository or checkout a different branch.
Usage:
git status - View an overview of your git repository.
git branch - View available branches.
git checkout main - Checkout the main branch of your code.
git pull - Pull the latest code from your current branch.
For updating evennia code, the same commands are available with 'git evennia'.
If there are any conflicts encountered, the command will abort. The command will reload your game after pulling new code automatically, but for changes involving persistent scripts etc, you may need to manually restart.
"""
key = "git"
locks = "cmd:pperm(Developer)"
help_category = "System"
directory = settings.GAME_DIR
repo_type = "game"
remote_link = "[your remote link]"
# CmdSet for easily install all commands
class GitCmdSet(CmdSet):
@ -169,3 +188,4 @@ class GitCmdSet(CmdSet):
def at_cmdset_creation(self):
self.add(CmdGit)
self.add(CmdGitEvennia)

View file

@ -3,10 +3,10 @@ Tests of git.
"""
from evennia import InterruptCommand
from django.conf import settings
from evennia.commands.default.tests import BaseEvenniaCommandTest
from evennia.utils.test_resources import EvenniaTest
from evennia.contrib.utils.git.git_integration import CmdGit
from evennia.contrib.utils.git_integration.git_integration import CmdGit, CmdGitEvennia
from evennia.utils.utils import list_to_string
import git
@ -16,7 +16,7 @@ import datetime
class TestCmdGit(CmdGit):
pass
class TestGit(EvenniaTest):
class TestGitIntegration(EvenniaTest):
@mock.patch("git.Repo")
@mock.patch("git.Git")
@mock.patch("git.Actor")
@ -72,4 +72,34 @@ class TestGit(EvenniaTest):
self.test_cmd_git.pull()
repo = self.test_cmd_git.repo
self.char1.msg.assert_called_with(f"You have pulled new code. Server restart initiated.|/Head now at {repo.git.rev_parse(repo.head.commit.hexsha, short=True)}.|/Author: {repo.head.commit.author.name} ({repo.head.commit.author.email})|/{repo.head.commit.message.strip()}")
class TestGitEvennia(BaseEvenniaCommandTest):
def setUp(self):
super().setUp()
try:
self.repo = git.Repo(settings.EVENNIA_DIR, search_parent_directories=True)
except git.exc.InvalidGitRepositoryError:
print("Test TestGitEvennia failed, unable to find Evennia directory.")
self.commit = self.repo.head.commit
self.branch = self.repo.active_branch.name
def test_git_evennia_status(self):
# View current git evennia status
time_of_commit = datetime.datetime.fromtimestamp(self.commit.committed_date)
status_msg = '\n'.join([f"Branch: {self.branch} ({self.repo.git.rev_parse(self.commit.hexsha, short=True)}) ({time_of_commit})",
f"By {self.commit.author.email}: {self.commit.message}"])
self.call(
CmdGitEvennia(),
"status",
status_msg
)
def test_git_evennia_branch(self):
# View current branch & branches available
remote_refs = self.repo.remote().refs
branch_msg = f"Current branch: {self.branch}. Branches available: {list_to_string(remote_refs)}"
self.call(
CmdGitEvennia(),
"branch",
branch_msg
)