Skip to content

Instantly share code, notes, and snippets.

@ctorgalson
Created July 23, 2019 19:56
Show Gist options
  • Save ctorgalson/2e8a2d24936dd5fd56677a424468d87d to your computer and use it in GitHub Desktop.
Save ctorgalson/2e8a2d24936dd5fd56677a424468d87d to your computer and use it in GitHub Desktop.
Simple Python script to simplify making git branches based on a Trello path and the current date.
#!/usr/bin/env python3
"""
This script:
- prompts the user for a Trello path,
- validates that the provided path fits the expected pattern,
- splits it at the `/`,
- replaces `-` with `_`,
- determines the week number,
- determines the current year,
- assembles the path parts and date info into a branch name (like
`2019_w30_34_global_site_chrome_colours_NbKMh09l`),
- uses git to check out a new branch with that name.
Suggested use:
- save file as e.g. `/usr/local/bin/gtb` (`gtb`: Git Trello Branch),
- make it executable: `chomd a+x /usr/local/bin/gtb`,
- run script: `gtb`,
- paste trello path into prompt,
- hit [Return],
- enjoy your new branch.
"""
import datetime, re, subprocess, sys
def main():
trello = input("Please enter the Trello path: ")
try:
""" Make sure this looks like part of a Trello path. """
path = re.fullmatch(r'[0-9A-Za-z-]+\/[0-9A-Za-z-]+', trello)
""" Proceed if we we got a match. """
if path != None:
""" The parts will be the card name and id respectively. """
parts = path.group(0).split("/")
""" We use this for both the year and the week number. """
today = datetime.date.today()
""" Replace dashes with underscores--note that we don't have to
concern ourselves with non-ascii characters because a) Trello
should have taken care of this when building the path, and b) the
check on the result of the re.fullmatch() call prevents us from
getting this far if there ARE any non-alphanumeric characters
aside from the expected slash and dashes. """
name = re.sub(r'-', '_', parts[1])
""" We can use this part as-is. """
hash = parts[0]
""" Assemble the vars into a branch name and then create it! """
branch = "{}_w{}_{}_{}".format(
today.year, today.isocalendar()[1], name, hash)
subprocess.run(["git", "checkout", "-b", branch])
else:
raise ValueError
except ValueError:
print("The argument did not match the expected pattern.")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment