def get_config(path, option=None): if os.path.isfile(path): path = os.path.dirname(path) cmd = ["git", "config"] if option is not None: cmd.extend(["--get", option]) else: cmd.extend(["-l"]) command = Command(cmd, path, env={"PAGER": ""}) out = command.run_sync() if option is not None: return out.strip("\n\t ") retval = {} for line in out.splitlines(): if "=" not in line: continue key, value = line.split("=", 1) retval[key.lower().strip()] = value.strip("\n\t ") if retval == {}: return None return retval
def get_previous_file_name(self, uri, rev, file_name): self._check_uri(uri) cmd = ["git", "log", "--format=%H", "--name-status", "--find-copies", "-n1", rev] command = Command(cmd, uri, env={"PAGER": ""}) try: output = command.run_sync() except: return None file_pattern = re.compile("^[ACDMRTUXB]{0,1}([MADT])[ \t]+(.*)$") file_move_pattern = re.compile("^[ACDMRTUXB]{0,1}([RC])[0-9]+[ \t]+(.*)[ \t]+(.*)$") for line in output.splitlines(): old_file_name = new_file_name = None match_file = file_pattern.match(line) if match_file: old_file_name = new_file_name = match_file.group(2) match_file_move = file_move_pattern.match(line) if match_file_move: old_file_name = match_file_move.group(2) new_file_name = match_file_move.group(3) if new_file_name == file_name: return old_file_name return None
def get_config (path, option = None): if os.path.isfile (path): path = os.path.dirname (path) cmd = ['git', 'config'] if option is not None: cmd.extend (['--get', option]) else: cmd.extend (['-l']) command = Command (cmd, path, env = {'PAGER' : ''}) out = command.run_sync () if option is not None: return out.strip ('\n\t ') retval = {} for line in out.splitlines (): if '=' not in line: continue key, value = line.split ('=', 1) retval[key.lower ().strip ()] = value.strip ('\n\t ') if retval == {}: return None return retval
def get_repository_from_path(path): if os.path.isfile(path): path = os.path.dirname(path) pattern = re.compile("^[ \t]*(checkout of)?(parent)? branch:(.*)$") uri = None try: cmd = ['bzr', 'info'] command = Command(cmd, path, env={'LC_ALL': 'C'}) out = command.run_sync() for line in out.splitlines(): match = pattern.match(line) if not match: continue uri = match.group(3).strip() break except CommandError: raise RepositoryInvalidWorkingCopy('"%s" does not appear to be a Bzr ' 'working copy' % path) if uri is None: raise RepositoryInvalidWorkingCopy( '"%s" does not appear to be a Bzr' ' working copy' % path ) return 'bzr', uri
def get_repository_from_path(path): if os.path.isfile(path): path = os.path.dirname(path) pattern = re.compile("^[ \t]*(checkout of)?(parent)? branch:(.*)$") uri = None try: cmd = ['bzr', 'info'] command = Command(cmd, path, env={'LC_ALL': 'C'}) out = command.run_sync() for line in out.splitlines(): match = pattern.match(line) if not match: continue uri = match.group(3).strip() break except CommandError: raise RepositoryInvalidWorkingCopy( '"%s" does not appear to be a Bzr working copy' % path) if uri is None: raise RepositoryInvalidWorkingCopy( '"%s" does not appear to be a Bzr working copy' % path) return 'bzr', uri
def get_config(path, option=None): if os.path.isfile(path): path = os.path.dirname(path) cmd = ['git', 'config'] if option is not None: cmd.extend(['--get', option]) else: cmd.extend(['-l']) command = Command(cmd, path, env={'PAGER': ''}) out = command.run_sync() if option is not None: return out.strip('\n\t ') retval = {} for line in out.splitlines(): if '=' not in line: continue key, value = line.split('=', 1) retval[key.lower().strip()] = value.strip('\n\t ') if retval == {}: return None return retval
def get_previous_commit (self, uri, rev, file_name): self._check_uri (uri) cmd = ['git', 'log', '--follow', '--format=%H', rev, '--', file_name] command = Command (cmd, uri, env = {'PAGER' : ''}) try: out = command.run_sync () return out.splitlines()[1].strip ('\n\t ') except: return None
def _get_git_version(self): if self.git_version is not None: return self.git_version cmd = ["git", "--version"] command = Command(cmd) out = command.run_sync() version = out.replace("git version ", "") self.git_version = tuple([i for i in version.split(".")[:4]]) return self.git_version
def _get_git_version (self): if self.git_version is not None: return self.git_version cmd = ['git', '--version'] command = Command (cmd) out = command.run_sync () version = out.replace ("git version ", "") self.git_version = tuple ([int (i) for i in version.split ('.')]) return self.git_version
def get_last_revision(self, uri): self._check_uri(uri) cmd = ['git', 'rev-list', 'HEAD^..HEAD'] command = Command(cmd, uri, env={'PAGER': ''}) try: out = command.run_sync() except: return None if out == "": return None return out.strip('\n\t ')
def get_last_revision(self, uri): self._check_uri(uri) cmd = ["git", "rev-list", "HEAD^..HEAD"] command = Command(cmd, uri, env={"PAGER": ""}) try: out = command.run_sync() except: return None if out == "": return None return out.strip("\n\t ")
def _checkout_branch(self, path, branch): self._check_uri(path) current, branches = self._get_branches(path) if branch in branches: if branches.index(branch) == current: return cmd = ["git", "checkout", branch] else: cmd = ["git", "checkout", "-b", branch, "origin/%s" % (branch)] command = Command(cmd, path) command.run()
def get_last_revision(self, uri): self._check_uri(uri) cmd = ['bzr', 'revno'] command = Command(cmd, uri) try: out = command.run_sync() except: return None if out == "": return None return out.strip('\n\t ')
def _checkout_branch(self, path, branch): self._check_uri(path) current, branches = self._get_branches(path) if branch in branches: if branches.index(branch) == current: return cmd = ['git', 'checkout', branch] else: cmd = ['git', 'checkout', '-b', branch, 'origin/%s' % (branch)] command = Command(cmd, path) command.run()
def _checkout_branch (self, path, branch): self._check_uri (path) current, branches = self._get_branches (path) if branch in branches: if branches.index (branch) == current: return cmd = ['git', 'checkout', branch] else: cmd = ['git', 'checkout', '-b', branch, 'origin/%s' % (branch)] command = Command (cmd, path) command.run ()
def get_last_revision (self, uri): self._check_uri (uri) cmd = ['git', 'rev-list', 'HEAD^..HEAD'] command = Command (cmd, uri, env = {'PAGER' : ''}) try: out = command.run_sync () except: return None if out == "": return None return out.strip ('\n\t ')
def blame(self, uri, rev=None, files=None, mc=False): # In cvs rev already includes the branch info # so no need for a branch parameter self._check_srcdir(uri) cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'annotate'] if rev is not None: cmd.extend(['-r', rev]) if os.path.isfile(uri): directory = os.path.dirname(uri) target = os.path.basename(uri) else: directory = uri target = '.' if files is not None: for file in files: cmd.append(file) else: cmd.append(target) command = Command(cmd, directory) self._run_command(command, BLAME)
def log(self, uri, rev=None, files=None): repo_uri = get_auth_info(uri)['uri'] self._check_uri(repo_uri) if os.path.isfile(repo_uri): cwd = os.path.dirname(repo_uri) target = '.' elif os.path.isdir(repo_uri): cwd = repo_uri target = '.' else: cwd = os.getcwd() target = repo_uri cmd = ['svn', '-v', 'log'] cmd = self._get_command_auth(cmd) if rev is not None: cmd.extend(['-r', rev]) if files is not None: if target != '.': cmd.append(target) for file in files: cmd.append(file) else: cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, LOG)
def checkout(self, module, rootdir, newdir=None, branch=None, rev=None): # branch doesn't make sense here module == branch if newdir is not None: srcdir = os.path.join(rootdir, newdir) elif newdir == '.': srcdir = rootdir else: srcdir = os.path.join(rootdir, module) if os.path.exists(srcdir): try: self.update(srcdir, rev) return except RepositoryInvalidWorkingCopy: # If srcdir is not a valid working copy, # continue with the checkout pass cmd = ['bzr', 'branch', self.uri] if newdir is not None: cmd.append(newdir) else: cmd.append(module) command = Command(cmd, rootdir) self._run_command(command, CHECKOUT)
def get_info(uri, user=None, passwd=None): if os.path.isdir(uri): path = uri uri = '.' else: path = '.' if (user is not None) and (passwd is not None): cmd = ['svn', 'info', uri, '--username', user, '--password', passwd] else: cmd = ['svn', 'info', uri] command = Command(cmd, path, env={'LC_ALL': 'C'}) out = run_command_sync(command) retval = {} for line in out.splitlines(): if ':' not in line: continue key, value = line.split(':', 1) retval[key.lower().strip()] = value.strip() if retval == {} or (uri in retval and retval[uri] == '(Not a valid URL)'): return None return retval
def checkout(self, module, rootdir, newdir=None, branch=None, rev=None): if newdir is not None: srcdir = os.path.join(rootdir, newdir) else: srcdir = rootdir if not os.path.exists(srcdir): os.makedirs(srcdir) if os.path.exists(module): tarball_path = module else: # Download module to rootdir filename = os.path.basename(module).split('?')[0] tarball_path = os.path.join(srcdir, filename) cmd = get_download_command(module, tarball_path, '/dev/stdout') if cmd is None: return command = Command(cmd, srcdir) self._run_command(command, CHECKOUT) if not os.path.exists(tarball_path): return # Unpack the tarball fe = create_file_extractor(tarball_path) fe.extract(srcdir)
def diff(self, uri, branch=None, revs=None, files=None): self._check_srcdir(uri) cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'diff', '-uN'] if revs is not None: for rev in revs: cmd.extend(['-r', rev]) if os.path.isfile(uri): cwd = os.path.dirname(uri) else: cwd = uri if files is not None: for file in files: cmd.append(file) else: cmd.append('.') command = Command(cmd, cwd) # If cvs is successful, it returns a successful status; if there is an error, # it prints an error message and returns a failure status. One # exception to this is the cvs diff command. It will return a successful status # if it found no differences, or a failure status if there were differences or if # there was an error. Because this behavior provides no good way to detect errors, # in the future it is possible that cvs diff will be changed to behave like # the other cvs commands. try: self._run_command(command, DIFF) except RepositoryCommandError, e: if e.returncode != 0 and not e.error: pass else: raise e
def ls(self, uri, rev=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) target = os.path.basename(uri) elif os.path.isdir(uri): cwd = uri target = '.' else: cwd = os.getcwd() target = uri cmd = ['svn', '-R', 'ls'] if rev is not None: if target == '.': cmd.extend(['-r', rev]) else: target += "@%s" % (rev) cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, LS)
def diff(self, uri, branch=None, revs=None, files=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) target = '.' elif os.path.isdir(uri): cwd = uri target = '.' else: target = uri cwd = os.getcwd() cmd = ['svn', 'diff'] if revs is not None: if len(revs) == 1: cmd.extend(['-r', revs[0]]) elif len(revs) > 1: cmd.extend(['-r', revs[0] + ':' + revs[1]]) if files is not None: for file in files: if target == '.': cmd.append(file) else: cmd.append(os.path.join(target, file)) else: cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, DIFF)
def log(self, uri, rev=None, files=None, branch=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) target = '.' elif os.path.isdir(uri): cwd = uri target = '.' else: cwd = os.getcwd() target = uri cmd = ['svn', '-v', 'log'] if rev is not None: cmd.extend(['-r', rev]) if files is not None: if target != '.': cmd.append(target) for file in files: cmd.append(file) else: cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, LOG)
def log(self, uri, rev=None, files=None, gitref=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) files = [os.path.basename(uri)] elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() cmd = [ 'git', 'log', '--topo-order', '--pretty=fuller', '--parents', '--name-status', '-M', '-C', '-c' ] # Git < 1.6.4 -> --decorate # Git = 1.6.4 -> broken # Git > 1.6.4 -> --decorate=full try: major, minor, micro = self._get_git_version() except ValueError: major, minor, micro, extra = self._get_git_version() if major <= 1 and minor < 6: cmd.append('--decorate') elif major <= 1 and minor == 6 and micro <= 4: cmd.append('--decorate') else: cmd.append('--decorate=full') if gitref: cmd.append(gitref) else: try: get_config(uri, 'remote.origin.url') if major <= 1 and minor < 8: cmd.append('origin') else: cmd.append('--remotes=origin') except CommandError: pass cmd.append('--all') if rev is not None: cmd.append(rev) if files: cmd.append('--') for file in files: cmd.append(file) elif cwd != uri: cmd.append(uri) command = Command(cmd, cwd, env={'PAGER': ''}) try: self._run_command(command, LOG) except RepositoryCommandError: pass
def ls(self, uri, rev=None): self._check_uri(uri) target = None if os.path.isfile(uri): cwd = os.path.dirname(uri) target = os.path.basename(uri) elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() if rev is None: try: get_config(uri, 'remote.origin.url') rev = 'origin/master' except CommandError: rev = 'HEAD' cmd = ['git', 'ls-tree', '--name-only', '--full-name', '-r', rev] if target is not None: cmd.append(target) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, LS)
def show(self, uri, rev=None): self._check_uri(uri) if os.path.isfile(uri): cwd = self.__get_root_dir(uri) target = uri[len(cwd):].strip("/") elif os.path.isdir(uri): cwd = uri target = None else: cwd = os.getcwd() target = None cmd = ['git', 'show', '--pretty=format:'] if rev is not None: cmd.append(rev) cmd.append("--") if target is not None: cmd.append(target) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, DIFF)
def blame(self, uri, rev=None, files=None, mc=False): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) files = [os.path.basename(uri)] elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() cmd = ['git', 'blame', '--root', '-l', '-t', '-f'] if mc: cmd.extend(['-M', '-C']) if rev is not None: cmd.append(rev) else: try: get_config(uri, 'remote.origin.url') cmd.append('origin/master') except CommandError: pass cmd.append('--') # Git doesn't support multiple files # we take just the first one cmd.append(files and files[0] or uri) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, BLAME)
def diff(self, uri, branch=None, revs=None, files=None): self._check_uri(uri) if os.path.isfile(uri): cwd = self.__get_root_dir(uri) files = [uri[len(cwd):].strip("/")] elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() cmd = ['git', 'diff'] if revs is not None: if len(revs) == 1: cmd.append(revs[0]) elif len(revs) > 1: cmd.append("%s..%s" % (revs[0], revs[1])) cmd.append("--") if files is not None: cmd.extend(files) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, DIFF)
def get_previous_commit_and_file_name(self, uri, rev, file_name): self._check_uri(uri) prev_file_name = self.get_previous_file_name(uri, rev, file_name) cmd = ["git", "log", "--format=%H", rev, "--", prev_file_name] command = Command(cmd, uri, env={"PAGER": ""}) try: output = command.run_sync() except: return None revisions = output.splitlines() if len(revisions) > 1: return (revisions[1], prev_file_name) else: return None
def _get_git_version(self): if self.git_version is not None: return self.git_version cmd = ['git', '--version'] command = Command(cmd) out = command.run_sync() # it could looks like: # git version 1.7.10.4 // 1.8.4.rc3 // 1.7.12.4 (Apple Git-37) version = out.replace("git version ", "") try: self.git_version = tuple([int(i) for i in version.split('.')]) except ValueError: self.git_version = tuple([int(i) for i in version.split('.')[0:3]]) return self.git_version
def get_last_revision (self, uri): self._check_srcdir (uri) if not os.path.isfile (uri): return None filename = os.path.basename (uri) path = os.path.dirname (uri) cmd = ['cvs', 'status', filename] command = Command (cmd, path) out = command.run_sync () retval = None for line in out.splitlines (): if "Working revision:" in line: retval = line.split (":", 1)[1].strip ().split ()[0] return retval
def get_last_revision(self, uri): self._check_srcdir(uri) if not os.path.isfile(uri): return None filename = os.path.basename(uri) path = os.path.dirname(uri) cmd = ['cvs', 'status', filename] command = Command(cmd, path) out = command.run_sync() retval = None for line in out.splitlines(): if "Working revision:" in line: retval = line.split(":", 1)[1].strip().split()[0] return retval
def is_ancestor(self, uri, rev1, rev2): self._check_uri(uri) version = self._get_git_version() if version[0] >= 1 and version[1] >= 8: # 'git merge-base --is-ancestor' is only supported after 1.8 cmd = ['git', 'merge-base', '--is-ancestor', rev1, rev2] command = Command(cmd, uri, env={'PAGER': ''}) try: command.run() return True except CommandError as e: if e.returncode == 1: return False else: raise e else: # Should we implement an workaround for git under 1.8 or # just have git 1.8 or later in prerequisites? # An workaround can be found at # http://stackoverflow.com/a/3006203/1305362 raise NotImplementedError
def update(self, uri, rev=None, force=False): if not force: self._check_uri(uri) cmd = ['svn', 'update'] if rev is not None: cmd.extend(['-r', rev]) cmd.append(uri) command = Command(cmd, env={'LC_ALL': 'C'}) self._run_command(command, UPDATE)
def is_ancestor(self, uri, rev1, rev2): self._check_uri(uri) version = self._get_git_version() if version[0] == 0 or (version[0] == 1 and version[1] < 8): # Should we implement an workaround for git under 1.8 or # just have git 1.8 or later in prerequisites? # An workaround can be found at # http://stackoverflow.com/a/3006203/1305362 raise NotImplementedError # 'git merge-base --is-ancestor' is only supported after 1.8 cmd = ['git', 'merge-base', '--is-ancestor', rev1, rev2] command = Command(cmd, uri, env={'PAGER': ''}) try: command.run() return True except CommandError as e: if e.returncode == 1: return False else: raise e
def update(self, uri, rev=None): self._check_uri(uri) #TODO: revision cmd = ['bzr', 'pull'] if os.path.isfile(uri): directory = os.path.dirname(uri) else: directory = uri command = Command(cmd, directory) self._run_command(command, UPDATE)
def rlog(self, module, rev=None, files=None): cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'rlog'] if rev is not None: cmd.extend(['-r', rev]) if files is not None: for file in files: cmd.append(os.path.join(module, file)) else: cmd.append(module) command = Command(cmd) self._run_command(command, LOG)
def update(self, uri, rev=None, force=False): repo_uri = get_auth_info(uri)['uri'] if not force: self._check_uri(repo_uri) cmd = ['svn', 'update'] cmd = self._get_command_auth(cmd) if rev is not None: cmd.extend(['-r', rev]) cmd.append(repo_uri) command = Command(cmd, env={'LC_ALL': 'C'}) self._run_command(command, UPDATE)
def _get_branches(self, path): cmd = ['git', 'branch'] command = Command(cmd, path) out = command.run_sync() patt = re.compile("^\*(.*)$") i = 0 current = 0 retval = [] for line in out.splitlines(): if line.startswith(self.uri): continue match = patt.match(line) if match: current = i retval.append(match.group(1).strip(' ')) else: retval.append(line.strip(' ')) i += 1 return current, retval
def _get_branches (self, path): cmd = ['git', 'branch'] command = Command (cmd, path) out = command.run_sync () patt = re.compile ("^\* (.*)$") i = 0 current = 0 retval = [] for line in out.splitlines (): if line.startswith (self.uri): continue match = patt.match (line) if match: current = i retval.append (match.group (1).strip (' ')) else: retval.append (line.strip (' ')) i += 1 return current, retval
def update(self, uri, rev=None): self._check_uri(uri) branch = rev if branch is not None: self._checkout_branch(uri, branch) cmd = ['git', 'pull'] if os.path.isfile(uri): directory = os.path.dirname(uri) else: directory = uri command = Command(cmd, directory) self._run_command(command, UPDATE)
def update(self, uri, rev=None): self._check_srcdir(uri) cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'update', '-P', '-d'] if rev is not None: cmd.extend(['-r', rev]) if os.path.isfile(uri): directory = os.path.dirname(uri) cmd.append(os.path.basename(uri)) else: directory = uri cmd.append('.') command = Command(cmd, directory) self._run_command(command, UPDATE)
def size(self, uri, rev=None): self._check_uri(uri) cmd = ['git', 'cat-file', '-s'] cwd = self.__get_root_dir(uri) target = uri[len(cwd):].strip("/") if rev is not None: target = "%s:%s" % (rev, target) else: target = "HEAD:%s" % (target) cmd.append(target) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, SIZE)