def _create(cls, repo, path, resolve, reference, force): """internal method used to create a new symbolic reference. If resolve is False,, the reference will be taken as is, creating a proper symbolic reference. Otherwise it will be resolved to the corresponding object and a detached symbolic reference will be created instead""" full_ref_path = cls.to_full_path(path) abs_ref_path = os.path.join(repo.git_dir, full_ref_path) # figure out target data target = reference if resolve: target = Object.new(repo, reference) if not force and os.path.isfile(abs_ref_path): target_data = str(target) if isinstance(target, SymbolicReference): target_data = target.path if not resolve: target_data = "ref: " + target_data if open(abs_ref_path, 'rb').read().strip() != target_data: raise OSError("Reference at %s does already exist" % full_ref_path) # END no force handling ref = cls(repo, full_ref_path) ref.reference = target return ref
def _get_object(self): """ Returns The object our ref currently refers to. Refs can be cached, they will always point to the actual object as it gets re-created on each query """ # have to be dynamic here as we may be a tag which can point to anything # Our path will be resolved to the hexsha which will be used accordingly return Object.new(self.repo, self.path)
def _set_reference(self, ref): """ Set ourselves to the given ref. It will stay a symbol if the ref is a Reference. Otherwise we try to get a commit from it using our interface. Strings are allowed but will be checked to be sure we have a commit """ write_value = None if isinstance(ref, SymbolicReference): write_value = "ref: %s" % ref.path elif isinstance(ref, Commit): write_value = ref.sha else: try: write_value = ref.commit.sha except AttributeError: sha = str(ref) try: obj = Object.new(self.repo, sha) if obj.type != "commit": raise TypeError("Invalid object type behind sha: %s" % sha) write_value = obj.sha except Exception: raise ValueError("Could not extract object from %s" % ref) # END end try string # END try commit attribute # if we are writing a ref, use symbolic ref to get the reflog and more # checking # Otherwise we detach it and have to do it manually if write_value.startswith('ref:'): self.repo.git.symbolic_ref(self.path, write_value[5:]) return # END non-detached handling path = self._get_path() directory = os.path.dirname(path) if not os.path.isdir(directory): os.makedirs(directory) fp = open(path, "wb") try: fp.write(write_value) finally: fp.close()