Skip to content

Instantly share code, notes, and snippets.

@s-c-p
Created November 23, 2023 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-c-p/425d817a25f9e96c5df236a73f550268 to your computer and use it in GitHub Desktop.
Save s-c-p/425d817a25f9e96c5df236a73f550268 to your computer and use it in GitHub Desktop.
"""
Commit with message "yo"
never commit untracked files
`git add` is user's responsibility. once added to tracker
has 2 settings
commitEvery=1 # minute
uploadEvery=10 # minute
singleDeveloper=True # default
works only when a `whatamIdoing` file exists and if not singleDeveloper then branch != main
"""
import os
import sys
try:
import git
except ModuleNotFoundError:
print("pip install GitPython")
exit(-1)
def _str2obj(tgt_path):
if not os.path.isabs(tgt_path):
raise RuntimeError("Please supply absolute path only") from None
repo = git.Repo(tgt_path)
return repo
def commit(tgt_path):
repo = _str2obj(tgt_path)
if repo.is_dirty():
ignore = set(repo.untracked_files)
changed = set(map(
lambda x: x.a_path,
repo.index.diff(None)
))
to_stage = list(changed - ignore)
if to_stage:
repo.index.add(to_stage)
repo.index.commit("autosave")
print("saved changes")
else:
print("repo clean, no action taken")
return
def push(tgt_path):
repo = _str2obj(tgt_path)
for rem in repo.remotes:
if rem.name == 'origin':
break
else:
rem = repo.remotes[0]
rem.push().raise_if_error()
return
if __name__ == '__main__':
action, path = sys.argv[:1]
if action == 'ct':
commit(path)
elif action == 'push':
push(path)
else:
print("USAGE:\n\tprog [ct|push] /path/to/gitRepo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment