def revset_fromgit(repo, subset, x): '''``fromgit()`` Select changesets that originate from Git. ''' args = revset.getargs(x, 0, 0, "fromgit takes no arguments") git = GitHandler(repo, repo.ui) return [r for r in subset if git.map_git_get(repo[r].hex()) is not None]
def write(self, *args, **kwargs): super(ext_ui, self).write(*args, **kwargs) # Changesets are printed twice in the current hg code, always with label log.changeset if kwargs.has_key('label') and kwargs['label'] == 'log.changeset' and len(args) and cached_repo: global changeset_re global cached_git if not changeset_re: changeset_re = re.compile('(\d+):\w+(\s*)$') match = changeset_re.search(args[0]) if match: # Parse out from the changeset string: The numeric local rev, and the line terminator # (So that we know if we need to print the git revision with a newline or not) rev, terminator = match.group(1,2) try: # Works in Mercurial 1.9.x from mercurial.templatefilters import hexfilter, short except: # Works in Mercurial 1.8.x from mercurial.templatefilters import filters hexfilter = filters["hex"] short = filters["short"] hgsha = cached_repo.lookup(int(rev)) # Ints are efficient on lookup if (hgsha): hgsha = hexfilter(hgsha) if not cached_git: cached_git = GitHandler(cached_repo, self) gitsha = cached_git.map_git_get(hgsha) else: # Currently this case is hit when you do hg outgoing. I'm not sure why. gitsha = None if gitsha: if terminator == '\n': # hg log, etc output = _("git-rev: %s\n") else: # hg sum output = "git:%s " super(ext_ui, self).write(output % (short(gitsha)), label='log.gitchangeset')
def __init__(self): self.config_file = os.path.dirname(os.path.abspath(__file__)) + '/config.json' config_data = self.load_configuration() self.command_runner = SystemCommandRunner() self.configuration = ConfigurationLoader(json_config_data=config_data) self.git_handler = GitHandler(project_path=self.configuration.get_project_path_value())
def do_POST(self): """ Reply to an HTTP POST """ if not self.__authenticate(): self.send_response(403) self.end_headers() return # Send response 202 Accepted # We've accepted the request and are processing it. self.send_response(202) try: post_data = json.loads( self.rfile.read( int(self.headers['Content-Length'])).decode('utf-8')) if post_data['action'] not in ('opened', 'synchronize'): # Pull Request is no longer open. # Reply with HTTP 409 Conflict self.send_response(409) self.end_headers() return handler = GitHandler(post_data) handler.clone() handler.pylint_and_comment(self.config) # Reply 201 Created, we're not using 200 OK # because in that case we would have to send the result of # processing as a reply. # Instead we've created a comment on Github. self.send_response(201) self.end_headers() except Exception as error: print('Something gone wrong:\n{}'.format(str(error))) self.send_response(500) self.end_headers()
def findoutgoing(self, remote, base=None, heads=None, force=False): if isinstance(remote, gitrepo): git = GitHandler(self, self.ui) base, heads = git.get_refs(remote.path) out, h = super(hgrepo, self).findoutgoing(remote, base, heads, force) return out else: #pragma: no cover return super(hgrepo, self).findoutgoing(remote, base, heads, force)
def getremotechanges(orig, ui, repo, other, revs, *args, **opts): if isinstance(other, gitrepo.gitrepo): git = GitHandler(repo, ui) r, c, cleanup = git.getremotechanges(other, revs) # ugh. This is ugly even by mercurial API compatibility standards if 'onlyheads' not in orig.func_code.co_varnames: cleanup = None return r, c, cleanup return orig(ui, repo, other, revs, *args, **opts)
def gitnodekw(**args): """:gitnode: String. The Git changeset identification hash, as a 40 hexadecimal digit string.""" node = args['ctx'] repo = args['repo'] git = GitHandler(repo, repo.ui) gitnode = git.map_git_get(node.hex()) if gitnode is None: gitnode = '' return gitnode
def getremotechanges(orig, ui, repo, other, *args, **opts): if isinstance(other, gitrepo.gitrepo): revs = opts.get('onlyheads', opts.get('revs')) git = GitHandler(repo, ui) r, c, cleanup = git.getremotechanges(other, revs) # ugh. This is ugly even by mercurial API compatibility standards if 'onlyheads' not in orig.func_code.co_varnames: cleanup = None return r, c, cleanup return orig(ui, repo, other, *args, **opts)
def push(self, remote, force=False, revs=None, newbranch=None): if isinstance(remote, gitrepo): git = GitHandler(self, self.ui) git.push(remote.path, revs, force) else: #pragma: no cover # newbranch was added in 1.6 if newbranch is None: return super(hgrepo, self).push(remote, force, revs) else: return super(hgrepo, self).push(remote, force, revs, newbranch)
def push(self, remote, force=False, revs=None, newbranch=None): if isinstance(remote, gitrepo): git = GitHandler(self, self.ui) # This is needed for bookmark deletion to work. remote.git_handler = git return git.push(remote.path, revs, force) else: #pragma: no cover # newbranch was added in 1.6 if newbranch is None: return super(hgrepo, self).push(remote, force, revs) else: return super(hgrepo, self).push(remote, force, revs, newbranch)
def findcommonoutgoing(orig, repo, other, *args, **kwargs): if isinstance(other, gitrepo.gitrepo): git = GitHandler(repo, repo.ui) heads = git.get_refs(other.path)[0] kw = {} kw.update(kwargs) for val, k in zip(args, ('onlyheads', 'force', 'commoninc', 'portable')): kw[k] = val force = kw.get('force', False) commoninc = kw.get('commoninc', None) if commoninc is None: commoninc = discovery.findcommonincoming(repo, other, heads=heads, force=force) kw['commoninc'] = commoninc return orig(repo, other, **kw) return orig(repo, other, *args, **kwargs)
def findoutgoing(orig, local, remote, *args, **kwargs): kw = {} kw.update(kwargs) for val, k in zip(args, ('base', kwname, 'force')): kw[k] = val if isinstance(remote, gitrepo.gitrepo): # clean up this cruft when we're 1.7-only, remoteheads and # the return value change happened between 1.6 and 1.7. git = GitHandler(local, local.ui) base, heads = git.get_refs(remote.path) newkw = {'base': base, kwname: heads} newkw.update(kw) kw = newkw if kwname == 'heads': r = orig(local, remote, **kw) return [x[0] for x in r] return orig(local, remote, **kw)
def _findtags(self): (tags, tagtypes) = super(hgrepo, self)._findtags() git = GitHandler(self, self.ui) for tag, rev in git.tags.iteritems(): tags[tag] = bin(rev) tagtypes[tag] = 'git' return (tags, tagtypes)
def revset_gitnode(repo, subset, x): '''``gitnode(hash)`` Select changesets that originate in the given Git revision. ''' args = revset.getargs(x, 1, 1, "gitnode takes one argument") rev = revset.getstring(args[0], "the argument to gitnode() must be a hash") git = GitHandler(repo, repo.ui) def matches(r): gitnode = git.map_git_get(repo[r].hex()) if gitnode is None: return False return rev in [gitnode, gitnode[:12]] return [r for r in subset if matches(r)]
def findoutgoing(orig, local, remote, *args, **kwargs): if isinstance(remote, gitrepo.gitrepo): hgver = hg.util.version() if hgver >= "1.8.9" or (hgver > "1.8" and "+" in hgver): raise hgutil.Abort("hg-git outgoing support is broken on hg 1.9.x") # clean up this cruft when we're 1.7-only, remoteheads and # the return value change happened between 1.6 and 1.7. kw = {} kw.update(kwargs) for val, k in zip(args, ("base", kwname, "force")): kw[k] = val git = GitHandler(local, local.ui) base, heads = git.get_refs(remote.path) newkw = {"base": base, kwname: heads} newkw.update(kw) kw = newkw if kwname == "heads": r = orig(local, remote, **kw) return [x[0] for x in r] if kwname == "onlyheads": del kw["base"] return orig(local, remote, **kw) return orig(local, remote, *args, **kwargs)
def findoutgoing(orig, local, remote, *args, **kwargs): if isinstance(remote, gitrepo.gitrepo): hgver = hg.util.version() if hgver >= '1.8.9' or (hgver > '1.8' and '+' in hgver): raise hgutil.Abort( 'hg-git outgoing support is broken on hg 1.9.x') # clean up this cruft when we're 1.7-only, remoteheads and # the return value change happened between 1.6 and 1.7. kw = {} kw.update(kwargs) for val, k in zip(args, ('base', kwname, 'force')): kw[k] = val git = GitHandler(local, local.ui) base, heads = git.get_refs(remote.path) newkw = {'base': base, kwname: heads} newkw.update(kw) kw = newkw if kwname == 'heads': r = orig(local, remote, **kw) return [x[0] for x in r] if kwname == 'onlyheads': del kw['base'] return orig(local, remote, **kw) return orig(local, remote, *args, **kwargs)
def gremote(ui, repo, *args): git = GitHandler(repo, ui) if len(args) == 0: git.remote_list() elif len(args) < 2: repo.ui.warn(_("must supply an action and a remote\n")) else: verb = args[0] nick = args[1] if verb == 'add': if len(args) == 3: git.remote_add(nick, args[2]) else: repo.ui.warn(_("must supply a url to add as a remote\n")) elif verb == 'rm': git.remote_remove(nick) elif verb == 'show': git.remote_show(nick) else: repo.ui.warn(_("unrecognized command to gremote\n"))
def tags(self): if not hasattr(self, 'tagscache'): # mercurial 1.4 tmp = super(hgrepo, self).tags() tmp.update(self.gitrefs()) return tmp if self.tagscache: return self.tagscache git = GitHandler(self, self.ui) tagscache = super(hgrepo, self).tags() for tag, rev in git.tags.iteritems(): if tag in tagscache: continue tagscache[tag] = bin(rev) self._tagstypecache[tag] = 'git' return tagscache
def tags(self): if hasattr(self, 'tagscache') and self.tagscache: # Mercurial 1.4 and earlier. return self.tagscache elif hasattr(self, '_tags') and self._tags: # Mercurial 1.5 and later. return self._tags git = GitHandler(self, self.ui) tagscache = super(hgrepo, self).tags() tagscache.update(self.gitrefs()) for tag, rev in git.tags.iteritems(): if tag in tagscache: continue tagscache[tag] = bin(rev) if hasattr(self, '_tagstypecache'): # Only present in Mercurial 1.3 and earlier. self._tagstypecache[tag] = 'git' return tagscache
def gclone(ui, git_url, hg_repo_path=None): # determine new repo name if not hg_repo_path: hg_repo_path = hg.defaultdest(git_url) if hg_repo_path.endswith('.git'): hg_repo_path = hg_repo_path[:-4] hg_repo_path += '-hg' dest_repo = hg.repository(ui, hg_repo_path, create=True) # fetch the initial git data git = GitHandler(dest_repo, ui) git.remote_add('origin', git_url) git.fetch('origin') # checkout the tip node = git.remote_head('origin') hg.update(dest_repo, node)
def __init__(self, ui, path, create=True): dest = hg.defaultdest(path) if dest.endswith('.git'): dest = dest[:-4] # create the local hg repo on disk dest_repo = hg.repository(ui, dest, create=True) # fetch the initial git data git = GitHandler(dest_repo, ui) git.remote_add('origin', path) git.fetch('origin') # checkout the tip node = git.remote_head('origin') hg.update(dest_repo, node) # exit to stop normal `hg clone` flow raise SystemExit
def gsummary(ui, repo): ctx = repo[None] parents = ctx.parents() git = GitHandler(repo, ui) for p in parents: repo.ui.status(_('git-rev: %s\n') % git.map_git_get(p.hex()))
def gclear(ui, repo): repo.ui.status(_("clearing out the git cache data\n")) git = GitHandler(repo, ui) git.clear()
def gexport(ui, repo): git = GitHandler(repo, ui) git.export_commits()
def gimport(ui, repo, remote_name=None): git = GitHandler(repo, ui) git.import_commits(remote_name)
def gfetch(ui, repo, remote_name='origin'): repo.ui.status(_("pulling from git url\n")) git = GitHandler(repo, ui) git.fetch(remote_name)
class Preprocessor: config_file = 'config.json' configuration = {} command_runner = None git_handler = None def __init__(self): self.config_file = os.path.dirname(os.path.abspath(__file__)) + '/config.json' config_data = self.load_configuration() self.command_runner = SystemCommandRunner() self.configuration = ConfigurationLoader(json_config_data=config_data) self.git_handler = GitHandler(project_path=self.configuration.get_project_path_value()) def execute(self): errors = self.validate() if errors.__len__() > 0: self.show_errors(errors) else: self.do_commit() def validate(self): files_to_check = self.get_files_to_check_list() if files_to_check.__len__() <= 0: return [] errors = {} filename = '' appendable = True files_to_check = " ".join(files_to_check) phpcs_run_command = self.configuration.get_codesniffer_path_value() + ' --standard=PSR2' + files_to_check phpcs_response = self.command_runner.execute(command=phpcs_run_command) break_point_list = self.get_break_points() for line in phpcs_response: if 'FILE:' in line: filename = line.split(':')[1].strip() if any(break_point in line for break_point in break_point_list): data = line.split('|') if data.__len__() == 3: if not errors.get(filename): errors[filename] = [] errors[filename].append({ 'line': data[0].strip(), 'message': data[2].strip() }) appendable = True elif line.split('|').__len__() > 1 and line.split('|')[0].isspace() and appendable: errors[filename][-1]['message'] += ' ' + line.split('|')[2].strip() else: appendable = False return errors def load_configuration(self): with open(self.config_file) as config_data_file: config_data = json.load(config_data_file) return config_data def get_files_to_check_list(self): files_to_check = [] add_to_check_list = False files_to_commit = self.git_handler.status() for line in files_to_commit: if 'Changes to be committed' in line: add_to_check_list = True elif 'Untracked files' in line or 'Changes not staged for commit' in line: add_to_check_list = False elif '.php~' in line: add_to_check_list = False if add_to_check_list is True and ('new file' in line or 'modified' in line): filename = self.configuration.get_project_path_value() + '/' + line.split(':')[1].strip() if self.configuration.get_ignore_view_files_value(): if any(view_files_extension in filename for view_files_extension in self.configuration.get_view_files_extensions_list()): continue if self.configuration.get_ignore_js_files_value(): if any(js_files_extension in filename for js_files_extension in self.configuration.get_js_files_extensions_list()): continue files_to_check.append(filename) return files_to_check @staticmethod def show_errors(self, errors): for file in errors: print('') print(file) error_list = errors.get(file) for error in error_list: print('- ' + error.get('message') + '(line: ' + error.get('line') + ')') def get_break_points(self): severity_level = self.configuration.get_severity_level_value() break_points = [Severity.ERROR] if severity_level == Severity.WARNING: break_points.append(Severity.WARNING) return break_points def do_commit(self): commit_message = sys.argv[1] commit_response = self.git_handler.commit(message=commit_message) for line in commit_response: print(line)
def pull(self, remote, heads=None, force=False): if isinstance(remote, gitrepo): git = GitHandler(self, self.ui) git.fetch(remote.path, heads) else: #pragma: no cover return super(hgrepo, self).pull(remote, heads, force)
def push(self, remote, force=False, revs=None): if isinstance(remote, gitrepo): git = GitHandler(self, self.ui) git.push(remote.path, revs, force) else: #pragma: no cover return super(hgrepo, self).push(remote, force, revs)
def githandler(self): '''get the GitHandler for an hg repo This only makes sense if the repo talks to at least one git remote. ''' return GitHandler(self, self.ui)
def pull(self, remote, heads=None, force=False): if isinstance(remote, gitrepo): git = GitHandler(self, self.ui) return git.fetch(remote.path, heads) else: #pragma: no cover return super(hgrepo, self).pull(remote, heads, force)
def _initializehandler(self): if self.handler is None and self.localrepo is not None: self.handler = GitHandler(self.localrepo, self.localrepo.ui) return self.handler
def gpush(ui, repo, remote_name='origin', branch=None): git = GitHandler(repo, ui) git.push(remote_name)
def get_git_handler(): git_handler = GitHandler(os.path.dirname(os.path.abspath(__file__))) return git_handler