handle git being in a detached head state when getting current commit
This commit is contained in:
parent
de7d35015e
commit
4e9df2c552
3 changed files with 19 additions and 14 deletions
|
@ -4,11 +4,12 @@ from src import IRCBot, ModuleManager, utils
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
@utils.hook("received.command.version")
|
@utils.hook("received.command.version")
|
||||||
def version(self, event):
|
def version(self, event):
|
||||||
commit_hash = utils.git_commit(self.bot.directory)
|
commit = utils.git_commit(self.bot.directory)
|
||||||
|
|
||||||
out = "Version: BitBot %s" % IRCBot.VERSION
|
out = "Version: BitBot %s" % IRCBot.VERSION
|
||||||
if not commit_hash == None:
|
if not commit == None:
|
||||||
out = "%s (%s@%s)" % (out, branch, commit_hash)
|
branch, commit = commit
|
||||||
|
out = "%s (%s@%s)" % (out, branch or "", commit)
|
||||||
event["stdout"].write(out)
|
event["stdout"].write(out)
|
||||||
|
|
||||||
@utils.hook("received.command.source")
|
@utils.hook("received.command.source")
|
||||||
|
|
|
@ -272,9 +272,10 @@ class ModuleManager(object):
|
||||||
for key, value in magic.get_exports():
|
for key, value in magic.get_exports():
|
||||||
context_exports.add(key, value)
|
context_exports.add(key, value)
|
||||||
|
|
||||||
current_commit = utils.git_commit(bot.directory)
|
branch, commit = utils.git_commit(bot.directory)
|
||||||
|
|
||||||
return LoadedModule(definition.name, module_title, module_object,
|
return LoadedModule(definition.name, module_title, module_object,
|
||||||
context, import_name, definition.is_core, commit=current_commit)
|
context, import_name, definition.is_core, commit=commit)
|
||||||
|
|
||||||
def load_module(self, bot: "IRCBot.Bot", definition: ModuleDefinition
|
def load_module(self, bot: "IRCBot.Bot", definition: ModuleDefinition
|
||||||
) -> LoadedModule:
|
) -> LoadedModule:
|
||||||
|
|
|
@ -127,17 +127,20 @@ def deadline_process(func: typing.Callable[[], DeadlineProcessReturnType],
|
||||||
else:
|
else:
|
||||||
raise out # type: ignore
|
raise out # type: ignore
|
||||||
|
|
||||||
def git_commit(bot_directory: str) -> typing.Optional[str]:
|
def git_commit(bot_directory: str
|
||||||
|
) -> typing.Tuple[typing.Optional[str], typing.Optional[str]]:
|
||||||
git_dir = os.path.join(bot_directory, ".git")
|
git_dir = os.path.join(bot_directory, ".git")
|
||||||
head_filepath = os.path.join(git_dir, "HEAD")
|
head_filepath = os.path.join(git_dir, "HEAD")
|
||||||
if os.path.isfile(head_filepath):
|
if os.path.isfile(head_filepath):
|
||||||
ref = None
|
|
||||||
with open(head_filepath, "r") as head_file:
|
with open(head_filepath, "r") as head_file:
|
||||||
ref = head_file.readline().split(" ", 1)[1].strip()
|
ref_line = head_file.readline().strip()
|
||||||
branch = ref.rsplit("/", 1)[1]
|
if not ref_line.startswith("ref: "):
|
||||||
|
return None, ref_line
|
||||||
|
else:
|
||||||
|
ref = ref_line.split(" ", 1)[1]
|
||||||
|
branch = ref.rsplit("/", 1)[1]
|
||||||
|
|
||||||
ref_filepath = os.path.join(git_dir, ref)
|
ref_filepath = os.path.join(git_dir, ref)
|
||||||
if os.path.isfile(ref_filepath):
|
with open(ref_filepath, "r") as ref_file:
|
||||||
with open(ref_filepath, "r") as ref_file:
|
return branch, ref_file.readline().strip()[:8]
|
||||||
return ref_file.readline().strip()[:8]
|
return None, None
|
||||||
return None
|
|
||||||
|
|
Loading…
Reference in a new issue