Skip to content

Commit 4e9df2c

Browse files
committed
handle git being in a detached head state when getting current commit
1 parent de7d350 commit 4e9df2c

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

modules/info.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
class Module(ModuleManager.BaseModule):
55
@utils.hook("received.command.version")
66
def version(self, event):
7-
commit_hash = utils.git_commit(self.bot.directory)
7+
commit = utils.git_commit(self.bot.directory)
88

99
out = "Version: BitBot %s" % IRCBot.VERSION
10-
if not commit_hash == None:
11-
out = "%s (%s@%s)" % (out, branch, commit_hash)
10+
if not commit == None:
11+
branch, commit = commit
12+
out = "%s (%s@%s)" % (out, branch or "", commit)
1213
event["stdout"].write(out)
1314

1415
@utils.hook("received.command.source")

src/ModuleManager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,10 @@ def _load_module(self, bot: "IRCBot.Bot", definition: ModuleDefinition,
272272
for key, value in magic.get_exports():
273273
context_exports.add(key, value)
274274

275-
current_commit = utils.git_commit(bot.directory)
275+
branch, commit = utils.git_commit(bot.directory)
276+
276277
return LoadedModule(definition.name, module_title, module_object,
277-
context, import_name, definition.is_core, commit=current_commit)
278+
context, import_name, definition.is_core, commit=commit)
278279

279280
def load_module(self, bot: "IRCBot.Bot", definition: ModuleDefinition
280281
) -> LoadedModule:

src/utils/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,20 @@ def _wrap(func, q):
127127
else:
128128
raise out # type: ignore
129129

130-
def git_commit(bot_directory: str) -> typing.Optional[str]:
130+
def git_commit(bot_directory: str
131+
) -> typing.Tuple[typing.Optional[str], typing.Optional[str]]:
131132
git_dir = os.path.join(bot_directory, ".git")
132133
head_filepath = os.path.join(git_dir, "HEAD")
133134
if os.path.isfile(head_filepath):
134-
ref = None
135135
with open(head_filepath, "r") as head_file:
136-
ref = head_file.readline().split(" ", 1)[1].strip()
137-
branch = ref.rsplit("/", 1)[1]
138-
139-
ref_filepath = os.path.join(git_dir, ref)
140-
if os.path.isfile(ref_filepath):
141-
with open(ref_filepath, "r") as ref_file:
142-
return ref_file.readline().strip()[:8]
143-
return None
136+
ref_line = head_file.readline().strip()
137+
if not ref_line.startswith("ref: "):
138+
return None, ref_line
139+
else:
140+
ref = ref_line.split(" ", 1)[1]
141+
branch = ref.rsplit("/", 1)[1]
142+
143+
ref_filepath = os.path.join(git_dir, ref)
144+
with open(ref_filepath, "r") as ref_file:
145+
return branch, ref_file.readline().strip()[:8]
146+
return None, None

0 commit comments

Comments
 (0)