def delete(cls, repo, path): """Delete the reference at the given path :param repo: Repository to delete the reference from :param path: Short or full path pointing to the reference, i.e. refs/myreference or just "myreference", hence 'refs/' is implied. Alternatively the symbolic reference to be deleted""" full_ref_path = cls.to_full_path(path) abs_path = join(repo.git_dir, full_ref_path) if exists(abs_path): os.remove(abs_path) else: # check packed refs pack_file_path = cls._get_packed_refs_path(repo) try: reader = open(pack_file_path, 'rb') except (OSError, IOError): pass # it didnt exist at all else: new_lines = list() made_change = False dropped_last_line = False for line in reader: # keep line if it is a comment or if the ref to delete is not # in the line # If we deleted the last line and this one is a tag-reference object, # we drop it as well if ( line.startswith('#') or full_ref_path not in line ) and \ (not dropped_last_line or dropped_last_line and not line.startswith('^')): new_lines.append(line) dropped_last_line = False continue # END skip comments and lines without our path # drop this line made_change = True dropped_last_line = True # END for each line in packed refs reader.close() # write the new lines if made_change: # write-binary is required, otherwise windows will # open the file in text mode and change LF to CRLF ! open(pack_file_path, 'wb').writelines(new_lines) # END write out file # END open exception handling # END handle deletion # delete the reflog reflog_path = RefLog.path(cls(repo, full_ref_path)) if os.path.isfile(reflog_path): os.remove(reflog_path)
def delete(cls, repo, path): """Delete the reference at the given path :param repo: Repository to delete the reference from :param path: Short or full path pointing to the reference, i.e. refs/myreference or just "myreference", hence 'refs/' is implied. Alternatively the symbolic reference to be deleted""" full_ref_path = cls.to_full_path(path) abs_path = join(repo.git_dir, full_ref_path) if exists(abs_path): os.remove(abs_path) else: # check packed refs pack_file_path = cls._get_packed_refs_path(repo) try: reader = open(pack_file_path, 'rb') except (OSError, IOError): pass # it didnt exist at all else: new_lines = list() made_change = False dropped_last_line = False for line in reader: # keep line if it is a comment or if the ref to delete is not # in the line # If we deleted the last line and this one is a tag-reference object, # we drop it as well if ( line.startswith('#') or full_ref_path not in line ) and \ ( not dropped_last_line or dropped_last_line and not line.startswith('^') ): new_lines.append(line) dropped_last_line = False continue # END skip comments and lines without our path # drop this line made_change = True dropped_last_line = True # END for each line in packed refs reader.close() # write the new lines if made_change: # write-binary is required, otherwise windows will # open the file in text mode and change LF to CRLF ! open(pack_file_path, 'wb').writelines(new_lines) # END write out file # END open exception handling # END handle deletion # delete the reflog reflog_path = RefLog.path(cls(repo, full_ref_path)) if os.path.isfile(reflog_path): os.remove(reflog_path)
def readable_db_object_path(self, hexsha): """ :return: readable object path to the object identified by hexsha :raise BadObject: If the object file does not exist""" try: return self._hexsha_to_file[hexsha] except KeyError: pass # END ignore cache misses # try filesystem path = self.db_path(self.object_path(hexsha)) if exists(path): self._hexsha_to_file[hexsha] = path return path # END handle cache raise BadObject(hexsha)
def _initialize(self, path): epath = abspath(expandvars(expanduser(path or os.getcwd()))) if not exists(epath): raise NoSuchPathError(epath) #END check file self._working_tree_dir = None self._git_path = None curpath = epath # walk up the path to find the .git dir while curpath: if is_git_dir(curpath): self._git_path = curpath self._working_tree_dir = os.path.dirname(curpath) break gitpath = join(curpath, self.repo_dir) if is_git_dir(gitpath): self._git_path = gitpath self._working_tree_dir = curpath break curpath, dummy = os.path.split(curpath) if not dummy: break # END while curpath if self._git_path is None: raise InvalidGitRepositoryError(epath) # END path not found self._bare = self._working_tree_dir is None if hasattr(self, 'config_reader'): try: self._bare = self.config_reader("repository").getboolean( 'core', 'bare') except Exception: # lets not assume the option exists, although it should pass #END handle exception #END check bare flag self._working_tree_dir = self._bare and None or self._working_tree_dir
def _initialize(self, path): epath = abspath(expandvars(expanduser(path or os.getcwd()))) if not exists(epath): raise NoSuchPathError(epath) #END check file self._working_tree_dir = None self._git_path = None curpath = epath # walk up the path to find the .git dir while curpath: if is_git_dir(curpath): self._git_path = curpath self._working_tree_dir = os.path.dirname(curpath) break gitpath = join(curpath, self.repo_dir) if is_git_dir(gitpath): self._git_path = gitpath self._working_tree_dir = curpath break curpath, dummy = os.path.split(curpath) if not dummy: break # END while curpath if self._git_path is None: raise InvalidGitRepositoryError(epath) # END path not found self._bare = self._working_tree_dir is None if hasattr(self, 'config_reader'): try: self._bare = self.config_reader("repository").getboolean('core','bare') except Exception: # lets not assume the option exists, although it should pass #END handle exception #END check bare flag self._working_tree_dir = self._bare and None or self._working_tree_dir