diff --git a/ext/system.py b/ext/system.py index e56bf23..83fee4c 100644 --- a/ext/system.py +++ b/ext/system.py @@ -17,26 +17,66 @@ async def get_git_status() -> dict: stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) - br_proc = await asyncio.create_subprocess_exec("git", "rev-parse", "--abbrev-ref", "HEAD", - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE - ) - dv_proc = await asyncio.create_subprocess_exec("git", "diff", "HEAD", - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE - ) stdout, _ = await id_proc.communicate() output["commit_id"] = stdout.decode("utf-8") - stdout, _ = await br_proc.communicate() - output["branch"] = stdout.decode("utf-8") + br_proc = await asyncio.create_subprocess_exec("git", "rev-parse", "--abbrev-ref", "HEAD", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) - stdout, _ = await dv_proc.communicate() - if stdout.decode("utf-8") == "": - output["dev"] = False - else: - output["dev"] = True + stdout, _ = await br_proc.communicate() + output["branch"] = stdout.decode("utf-8").strip() + + rm_proc = await asyncio.create_subprocess_exec("git", "branch", "-r", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + + stdout, _ = await rm_proc.communicate() + output['remote'] = "remotes/"+stdout.decode("utf-8").lstrip().split("\n")[0] + + # diff HEAD to remote + hd_proc = await asyncio.create_subprocess_exec("git", "diff", "HEAD", output['remote'], + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + + # diff index to remote + ix_proc = await asyncio.create_subprocess_exec("git", "diff", output['remote'], + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + stdout, _ = await ix_proc.communicate() + ix_diff = stdout.decode("utf-8") + + # commits ahead/behind remote + ab_proc = await asyncio.create_subprocess_exec("git", "rev-list", "--left-right", "--count", output['branch']+"..."+output["remote"], + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + stdout, _ = await ab_proc.communicate() + commits_ahead = stdout.decode("utf-8").split()[0] + commits_behind = stdout.decode("utf-8").split()[1] + + stdout, _ = await hd_proc.communicate() + hd_diff = stdout.decode("utf-8") + output['dev'] = False + + # dev environment tests + if hd_diff != "": # difference between HEAD and remote + output['dev'] = True + elif config.branch != output["branch"]: # branch has been changed + output['dev'] = True + elif ix_diff != "": # difference between index and remote + output['dev'] = True + elif int(commits_ahead) > 0: # ahead in commits + output['dev'] = True + + output['update'] = False + if not output['dev'] and int(commits_behind) > 0: + output['update'] = True except subprocess.SubprocessError as err: print(err) return output @@ -56,10 +96,12 @@ async def ping(ctx: lightbulb.Context) -> None: async def info(ctx: lightbulb.Context) -> None: git_status = await get_git_status() embed = hikari.Embed(title="About Me!") - embed.add_field("GitHub", config.git_url) + embed.add_field("Git Repository", config.git_url) embed.add_field("Version", git_status['commit_id'], inline=True) embed.add_field("Branch", git_status['branch'], inline=True) + embed.add_field("Remote", git_status['remote'], inline=True) embed.add_field("In Dev-Env?", git_status['dev']) + embed.add_field("Needs Update?", git_status['update']) await ctx.respond(embed) def load(bot: lightbulb.BotApp):