Skip to content

Commit

Permalink
Add clean command to remove zaphod generated branches
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayankur31 committed Oct 1, 2020
1 parent 1981076 commit 4b2e8c7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,11 @@ Usage
-c, --citations Document contains citations. Will add -bibtex to
latexmk.
Default: True
Subcommand: 'clean'
usage: zaphod clean [-h] [-y]
optional arguments:
-h, --help show this help message and exit
-y, --yes Assume yes Please be careful when using this option. Default: False
68 changes: 58 additions & 10 deletions zaphod.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ def __init__(self):
self.commandList = ["pdflatex", "latexdiff", "git"]
self.timenow = datetime.datetime.strftime(datetime.datetime.today(),
"%Y%m%d%H%M")
self.rev1Branch = self.timenow + "-latexdiff-rev1"
self.rev2Branch = self.timenow + "-latexdiff-rev2"
self.finalBranch = self.timenow + "-latexdiff-annotated"
self.branchSpec = "-zaphod-"
self.rev1Branch = self.timenow + self.branchSpec + "rev1"
self.rev2Branch = self.timenow + self.branchSpec + "rev2"
self.finalBranch = self.timenow + self.branchSpec + "annotated"

self.filelist = []
self.rev1filelist = []
self.rev2filelist = []
self.modifiedfiles = []

self.gitResetCommand = "git reset HEAD --hard".split()
self.gitCheckoutCommand = "git checkout".split()
self.gitCheckoutCommand = "git checlatexdiffkout".split()
self.gitAddCommand = "git add .".split()
self.gitCommitCommand = "git commit -m".split()
self.gitBranchCommand = "git branch".split()
self.gitBranchDeleteCommand = "git branch -D".split()
self.latexmkCleanCommand = ('latexmk -C'.split())
self.latexmkCommand = (
'latexmk -pdf -recorder'.split() + '-synctex=1 -use-make'.split() +
Expand Down Expand Up @@ -217,7 +220,7 @@ def diff(self, args):
os.remove(self.rev1filelist[i])
os.remove(self.rev2filelist[i])

self.generate_pdf("diff-" + self.optionsDict['rev1'] + "-" +
self.generate_pdf("zaphod-diff-" + self.optionsDict['rev1'] + "-" +
self.optionsDict['rev2'])

subprocess.call(self.gitAddCommand)
Expand Down Expand Up @@ -455,6 +458,35 @@ def revise(self, args):
self.generate_pdf("accepted")
self.save_changes()

def clean(self, args):
"""
Remove all branches created by Zaphod.
"""
self.zprint("Getting branch list.")
command = (self.gitBranchCommand)
ps = subprocess.check_output(command)

branches = ps.decode("ascii").split('\n')
zaphodBranches = 0
for line in branches:
branchName = line.strip().replace("* ", "")
if self.branchSpec in branchName:
zaphodBranches += 1
self.zprint(f"Found a zaphod branch: {branchName}")
command = (self.gitBranchDeleteCommand + [branchName])
if self.optionsDict['yes']:
self.zprint(f"Deleting branch {branchName}")
subprocess.call(command)
else:
deletebranch = input("Delete branch? Y/y/N/n: ")
if deletebranch == "Y" or deletebranch == "y":
subprocess.call(command)
else:
self.zprint(f"Skipping branch {branchName}")

if zaphodBranches == 0:
self.zprint("No Zaphod branches found.")

def remove_preamble(self):
"""Remove latexdiff preamble when all files have been revised."""
# Confirm that no files now have annotations
Expand Down Expand Up @@ -722,6 +754,21 @@ def setup(self):
Default: True"
)

self.clean_parser = self.subparser.add_parser(
"clean",
formatter_class=argparse.RawDescriptionHelpFormatter,
help="Clean up Zaphod related branches\n",
)
self.clean_parser.set_defaults(func=self.clean)
self.clean_parser.add_argument("-y", "--yes",
action="store_true",
default=False,
help="Assume yes \
Please be careful when using \
this option. \
Default: False"
)

def check_setup(self):
"""Check if Git directory is clean."""
command = "git status --porcelain".split()
Expand All @@ -738,15 +785,15 @@ def check_setup(self):
file=sys.stderr)
sys.exit(-3)

if self.optionsDict['subdir'] and not \
os.path.isdir(self.optionsDict['subdir']):
if 'subdir' in self.optionsDict and self.optionsDict['subdir'] \
and not os.path.isdir(self.optionsDict['subdir']):
print("Specified subdirectory not found at {}!\n".format(
self.optionsDict['subdir']) +
"Please check your arguments.", file=sys.stderr)
sys.exit(-4)

if self.optionsDict['main'] and self.optionsDict['subdir'] \
and not \
if 'main' in self.optionsDict and self.optionsDict['main'] \
and self.optionsDict['subdir'] and not \
os.path.isfile(os.path.join(self.optionsDict['subdir'],
self.optionsDict['main'])):
print("Specified main file not found at {}!\n".format
Expand All @@ -762,7 +809,8 @@ def check_setup(self):
file=sys.stderr)
sys.exit(-5)

if self.optionsDict['citations'] and not shutil.which("bibtex"):
if 'citations' in self.optionsDict and \
self.optionsDict['citations'] and not shutil.which("bibtex"):
print("bibtex not found! Exiting!",
file=sys.stderr)
sys.exit(-6)
Expand Down

0 comments on commit 4b2e8c7

Please sign in to comment.