def _read_config_file(self, path): """Read a .gitconfig file into a dict""" config = {} header_simple = re.compile(r"^\[(\s+)]$") header_subkey = re.compile(r'^\[(\s+) "(\s+)"\]$') with core.xopen(path, "rt") as f: file_lines = f.readlines() stripped_lines = [line.strip() for line in file_lines] lines = [line for line in stripped_lines if bool(line)] prefix = "" for line in lines: if line.startswith("#"): continue match = header_simple.match(line) if match: prefix = match.group(1) + "." continue match = header_subkey.match(line) if match: prefix = match.group(1) + "." + match.group(2) + "." continue k, v = _config_key_value(line, "=") k = prefix + k self._map[k.lower()] = k config[k] = v return config
def _read_config_file(self, path): """Read a .gitconfig file into a dict""" config = {} header_simple = re.compile(r'^\[(\s+)]$') header_subkey = re.compile(r'^\[(\s+) "(\s+)"\]$') with core.xopen(path, 'rt') as f: file_lines = f.readlines() stripped_lines = [line.strip() for line in file_lines] lines = [line for line in stripped_lines if bool(line)] prefix = '' for line in lines: if line.startswith('#'): continue match = header_simple.match(line) if match: prefix = match.group(1) + '.' continue match = header_subkey.match(line) if match: prefix = match.group(1) + '.' + match.group(2) + '.' continue k, v = _config_key_value(line, '=') k = prefix + k self._map[k.lower()] = k config[k] = v return config
def _read_config_file(self, path): """Read a .gitconfig file into a dict""" config = {} header_simple = re.compile(r'^\[(\s+)]$') header_subkey = re.compile(r'^\[(\s+) "(\s+)"\]$') with core.xopen(path, 'rt') as f: lines = filter(bool, [line.strip() for line in f.readlines()]) prefix = '' for line in lines: if line.startswith('#'): continue match = header_simple.match(line) if match: prefix = match.group(1) + '.' continue match = header_subkey.match(line) if match: prefix = match.group(1) + '.' + match.group(2) + '.' continue k, v = _config_key_value(line, '=') k = prefix + k self._map[k.lower()] = k config[k] = v return config
def write_json(values, path): try: parent = os.path.dirname(path) if not core.isdir(parent): core.makedirs(parent) with core.xopen(path, 'wt') as fp: json.dump(values, fp, indent=4) except: sys.stderr.write('git-cola: error writing "%s"\n' % path)
def _load(self): path = self.path() if not core.exists(path): return self._load_dot_cola() try: fp = core.xopen(path, 'rt') return mkdict(json.load(fp)) except: # bad json return {}
def save(self): path = self.path() try: parent = os.path.dirname(path) if not core.isdir(parent): core.makedirs(parent) with core.xopen(path, 'wb') as fp: json.dump(self.values, fp, indent=4) except: sys.stderr.write('git-cola: error writing "%s"\n' % path)
def do(self): fp = core.xopen(self.filename, 'wb') cmd = ['git', 'archive', '--format='+self.fmt] if self.fmt in ('tgz', 'tar.gz'): cmd.append('-9') if self.prefix: cmd.append('--prefix=' + self.prefix) cmd.append(self.ref) proc = core.start_command(cmd, stdout=fp) out, err = proc.communicate() fp.close() status = proc.returncode Interaction.log_status(status, out or '', err or '')
def do(self): fp = core.xopen(self.filename, "wb") cmd = ["git", "archive", "--format=" + self.fmt] if self.fmt in ("tgz", "tar.gz"): cmd.append("-9") if self.prefix: cmd.append("--prefix=" + self.prefix) cmd.append(self.ref) proc = core.start_command(cmd, stdout=fp) out, err = proc.communicate() fp.close() status = proc.returncode Interaction.log_status(status, out or "", err or "")
def do(self): model = self.model cmd = ["git", "show", "%s:%s" % (model.ref, model.relpath)] with core.xopen(model.filename, "wb") as fp: proc = core.start_command(cmd, stdout=fp) out, err = proc.communicate() status = proc.returncode msg = N_('Saved "%(filename)s" from "%(ref)s" to "%(destination)s"') % dict( filename=model.relpath, ref=model.ref, destination=model.filename ) Interaction.log_status(status, msg, "") Interaction.information(N_("File Saved"), N_('File saved to "%s"') % model.filename)
def do(self): model = self.model cmd = ['git', 'show', '%s:%s' % (model.ref, model.relpath)] with core.xopen(model.filename, 'wb') as fp: proc = core.start_command(cmd, stdout=fp) out, err = proc.communicate() status = proc.returncode msg = (N_('Saved "%(filename)s" from "%(ref)s" to "%(destination)s"') % dict(filename=model.relpath, ref=model.ref, destination=model.filename)) Interaction.log_status(status, msg, '') Interaction.information(N_('File Saved'), N_('File saved to "%s"') % model.filename)
def is_conflict_free(path): """Return True if `path` contains no conflict markers """ rgx = re.compile(r"^(<<<<<<<|\|\|\|\|\|\|\||>>>>>>>) ") try: with core.xopen(path, "r") as f: for line in f: line = core.decode(line, errors="ignore") if rgx.match(line): if should_stage_conflicts(path): return True else: return False except IOError: # We can't read this file ~ we may be staging a removal pass return True
def _load_dot_cola(self): values = {} path = os.path.join(core.expanduser('~'), '.cola') if not core.exists(path): return {} try: with core.xopen(path, 'r') as fp: json_values = json.load(fp) except: # bad json return {} # Keep only the entries we care about for key in self.values: try: values[key] = json_values[key] except KeyError: pass return values
def read_json(path): try: with core.xopen(path, 'rt') as fp: return mkdict(json.load(fp)) except: # bad path or json return {}