From 91686c4272aab3c1a2f05f55bb96f682a13557a7 Mon Sep 17 00:00:00 2001 From: axzn Date: Thu, 18 Jun 2020 23:43:05 +0200 Subject: [PATCH] support powershell as target shell --- .gitignore | 1 + README.md | 4 ++-- gitfiti.py | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index b4d274e08..3caa0d8f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc gitfiti.sh +gitfiti.ps1 diff --git a/README.md b/README.md index 1c33d5cf0..891eed41b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ An example of gitfiti in the wild: `gitfiti.py` is a tool I wrote to decorate your github account's commit history calendar by (blatantly) abusing git's ability to accept commits _in the past_. -How? `gitfiti.py` generates a bash script: `gitfiti.sh` that makes commits with the GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables set for each targeted pixel. +How? `gitfiti.py` generates a script that makes commits with the GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables set for each targeted pixel. Since this is likely to clobber repo's history, I highly recommend that you create a _new_ github repo when using gitfiti. Also, the generated bash script assumes you are using public-key authentication with git. @@ -19,7 +19,7 @@ Included "art" from left to right: kitty, oneup, oneup2, hackerschool, octocat, ### Usage: 1. Create a new github repo to store your handiwork. 2. Run `gitfiti.py` and follow the prompts for username, art selection, offset, and repo name. -3. Run the generated `gitfiti.sh` from your home directory (or any non-git tracked dir) and watch it go to work. +3. Run the generated `gitfiti.sh` or `gitfiti.ps1` from your home directory (or any non-git tracked dir) and watch it go to work. 4. Wait... Seriously, you'll probably need to wait a day or two for the gitfiti to show in your commit graph. ### User Templates diff --git a/gitfiti.py b/gitfiti.py index eeb42a39c..005e63446 100755 --- a/gitfiti.py +++ b/gitfiti.py @@ -197,6 +197,10 @@ IMAGES = { 'gliders': GLIDERS, } +SHELLS = { + 'bash': 'sh', + 'powershell': 'ps1', +} def load_images(img_names): """loads user images from given file(s)""" @@ -303,16 +307,24 @@ def generate_values_in_date_order(image, multiplier=1): yield image[h][w] * multiplier -def commit(commitdate): - template = ( +def commit(commitdate, shell): + template_bash = ( '''GIT_AUTHOR_DATE={0} GIT_COMMITTER_DATE={1} ''' '''git commit --allow-empty -m "gitfiti" > /dev/null\n''' ) + + template_powershell = ( + '''$Env:GIT_AUTHOR_DATE="{0}"\n$Env:GIT_COMMITTER_DATE="{1}"\n''' + '''git commit --allow-empty -m "gitfiti" | Out-Null\n''' + ) + + template = template_bash if shell == 'bash' else template_powershell + return template.format(commitdate.isoformat(), commitdate.isoformat()) -def fake_it(image, start_date, username, repo, git_url, offset=0, multiplier=1): - template = ( +def fake_it(image, start_date, username, repo, git_url, shell, offset=0, multiplier=1): + template_bash = ( '#!/usr/bin/env bash\n' 'REPO={0}\n' 'git init $REPO\n' @@ -327,11 +339,28 @@ def fake_it(image, start_date, username, repo, git_url, offset=0, multiplier=1): 'git push -u origin master\n' ) + template_powershell = ( + 'cd $PSScriptRoot\n' + '$REPO="{0}"\n' + 'git init $REPO\n' + 'cd $REPO\n' + 'New-Item README.md -ItemType file | Out-Null\n' + 'git add README.md\n' + 'New-Item gitfiti -ItemType file | Out-Null\n' + 'git add gitfiti\n' + '{1}\n' + 'git remote add origin {2}:{3}/$REPO.git\n' + 'git pull origin master\n' + 'git push -u origin master\n' + ) + + template = template_bash if shell == 'bash' else template_powershell + strings = [] for value, date in zip(generate_values_in_date_order(image, multiplier), generate_next_dates(start_date, offset)): for _ in range(value): - strings.append(commit(date)) + strings.append(commit(date, shell)) return template.format(repo, ''.join(strings), git_url, username) @@ -413,12 +442,18 @@ def main(): git_url = 'git@github.com' else: git_url = request_user_input('Enter Git URL like git@site.github.com: ') + + shell = '' + while shell not in SHELLS.keys(): + shell = request_user_input( + 'Enter the target shell ({}): '.format(' or '.join(SHELLS.keys()))) - output = fake_it(image, start_date, username, repo, git_url, offset, + output = fake_it(image, start_date, username, repo, git_url, shell, offset, fake_it_multiplier) - save(output, 'gitfiti.sh') - print('gitfiti.sh saved.') + output_filename = 'gitfiti.{}'.format(SHELLS[shell]) + save(output, output_filename) + print('{} saved.'.format(output_filename)) print('Create a new(!) repo named {0} at {1} and run the script'.format(repo, git_base))