Add autoload of colors to prompt.sh. Remove git_replace_author.py add matcher.py
This commit is contained in:
parent
ab8df9a1e8
commit
1c64644cf7
@ -1,107 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
from collections import namedtuple
|
||||
import argparse
|
||||
import re
|
||||
|
||||
from iterpipes import *
|
||||
|
||||
|
||||
CommitInfo = namedtuple('CommitInfo', ['hash', 'author', 'email'])
|
||||
|
||||
|
||||
class AlwaysMatches(object):
|
||||
|
||||
@classmethod
|
||||
def matches(*args):
|
||||
return True
|
||||
|
||||
|
||||
class GitAuthorReplacer(object):
|
||||
|
||||
def __init__(self, author_string, email_string,
|
||||
source_branch='HEAD', commit_matcher=AlwaysMatches):
|
||||
self.author_string = author_string
|
||||
self.email_string = email_string
|
||||
self.source_branch = source_branch
|
||||
self.commit_matcher = commit_matcher
|
||||
|
||||
@property
|
||||
def log_triples(self):
|
||||
return map(self.build_hash_author_email,
|
||||
run(linecmd('git log --pretty=format:"%h|%an|%ae"')))
|
||||
|
||||
@staticmethod
|
||||
def build_hash_author_email(line):
|
||||
line = line.strip()
|
||||
print line
|
||||
print line.split('|')
|
||||
return CommitInfo(*line.split('|'))
|
||||
|
||||
def recredit_commits(self):
|
||||
reversed_triples = self.log_triples[::-1]
|
||||
self.reset(reversed_triples[0])
|
||||
self.conditionally_recredit_commit(reversed_triples[0])
|
||||
for commit_info in reversed_triples[1:]:
|
||||
self.cherry_pick(commit_info)
|
||||
self.conditionally_recredit_commit(commit_info)
|
||||
|
||||
@staticmethod
|
||||
def reset(commit_info):
|
||||
return list(run(cmd('git reset --hard {}', commit_info.hash)))
|
||||
|
||||
@staticmethod
|
||||
def cherry_pick(commit_info):
|
||||
return list(run(cmd('git cherry-pick {}', commit_info.hash)))
|
||||
|
||||
def conditionally_recredit_commit(self, commit_info):
|
||||
if self.commit_matcher.matches(commit_info):
|
||||
return list(
|
||||
run(cmd('git commit --amend --author "{} {}" -C HEAD', self.author_string, '<{0}>'.format(self.email_string)))
|
||||
)
|
||||
|
||||
|
||||
class NameMatcher(object):
|
||||
|
||||
def __init__(self, name_regexp):
|
||||
self.matcher = re.compile(name_regexp)
|
||||
|
||||
def matches(self, commit_info):
|
||||
return bool(self.matcher.search(commit_info.author))
|
||||
|
||||
|
||||
class AndMatcher(object):
|
||||
|
||||
def __init__(self, *matchers):
|
||||
self.matchers = matchers
|
||||
|
||||
def matches(self, commit_info):
|
||||
return all(matcher.matches(commit_info) for matcher in self.matchers)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
action='store',
|
||||
type=str,
|
||||
dest='name',
|
||||
default='',
|
||||
help='Name to set for matching commits.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--email',
|
||||
action='store',
|
||||
type=str,
|
||||
dest='email',
|
||||
default='',
|
||||
help='Email to set for matching commits.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--name-re',
|
||||
action='append',
|
||||
dest='name_regexps',
|
||||
help='A regular expression to use when inspecting filepaths'
|
||||
)
|
||||
namespace = parser.parse_args()
|
||||
name_matchers = map(NameMatcher, namespace.name_regexps)
|
||||
GitAuthorReplacer(namespace.name, namespace.email, commit_matcher=AndMatcher(*name_matchers)).recredit_commits()
|
28
dotfiles/lib/python/matcher.py
Normal file
28
dotfiles/lib/python/matcher.py
Normal file
@ -0,0 +1,28 @@
|
||||
import re
|
||||
|
||||
|
||||
class LeafMatcher(object):
|
||||
|
||||
def __init__(self, regexp):
|
||||
self.matcher = re.compile(regexp)
|
||||
|
||||
def matches(self, string):
|
||||
return bool(self.matcher.search(string))
|
||||
|
||||
|
||||
class AndMatcher(object):
|
||||
|
||||
def __init__(self, *matchers):
|
||||
self.matchers = matchers
|
||||
|
||||
def matches(self, string):
|
||||
return all(matcher.matches(string) for matcher in self.matchers)
|
||||
|
||||
|
||||
class OrMatcher(object):
|
||||
|
||||
def __init__(self, *matchers):
|
||||
self.matchers = matchers
|
||||
|
||||
def matches(self, string):
|
||||
return any(matcher.matches(string) for matcher in self.matchers)
|
@ -1,3 +1,4 @@
|
||||
autoload -U colors && colors
|
||||
function current_directory() {
|
||||
local PWD=$(pwd)
|
||||
echo "${PWD/#$HOME/~}"
|
||||
|
Loading…
Reference in New Issue
Block a user