def clone_repo_and_checkout_tag(localRepo, bookid, tagname, filename, dest_path): repo_home = pathof(localRepo) repo_home = repo_home.replace("/", os.sep) repo_path = os.path.join(repo_home, "epub_" + bookid) dest_path = dest_path.replace("/", os.sep) cdir = os.getcwd() # first verify both repo and tagname exist taglst = [] if os.path.exists(repo_path): if not os.path.exists(dest_path): return "" os.chdir(repo_path) tags = porcelain.list_tags(repo='.') for atag in tags: taglst.append(unicode_str(atag)) # use dest_path to clone into # clone current repo "s" into new repo "r" with open_repo_closing(".") as s: s.clone(dest_path, mkdir=False, bare=False, origin=b"origin", checkout=False) # cd out **before** the repo closes os.chdir(dest_path) with open_repo_closing(".") as r: if tagname not in taglist or tagname == "HEAD": tagkey = utf8_str("HEAD") else: tagkey = utf8_str("refs/tags/" + tagname) refkey = tagkey # if annotated tag get the commit id it pointed to instead if isinstance(r[tagkey], Tag): refkey = r[tagkey].object[1] r.reset_index(r[refkey].tree) r.refs.set_symbolic_ref(b"HEAD", tagkey) # cd out **before** the repo closes os.chdir(cdir) return "success"
def generate_epub_from_tag(localRepo, bookid, tagname, filename, dest_path): repo_home = pathof(localRepo) repo_home = repo_home.replace("/", os.sep) repo_path = os.path.join(repo_home, "epub_" + bookid) cdir = os.getcwd() # first verify both repo and tagname exist epub_filepath = "" epub_name = filename + "_" + tagname + ".epub" taglst = [] if os.path.exists(repo_path): os.chdir(repo_path) tags = porcelain.list_tags(repo='.') os.chdir(cdir) for atag in tags: taglst.append(unicode_str(atag)) if tagname not in taglst: return epub_file_path # FIXME: there should **never** be unstaged changes or untracked files # in the repo because of how Sigil handles it, but we really should use # dulwich status to verify that before proceeding and abort otherwise. # Just in case the user uses command line git to manipulate the repo # outside of Sigil's control leaving it in a dirty state # Instead of cloning an entire repo just to do a checkout # of a tag, do all work in the current repo checkout_tag(repo_path, tagname) # working directory of the repo should now be populated epub_filepath = os.path.join(dest_path, epub_name) try: build_epub_from_folder_contents(repo_path, epub_filepath) except Exception as e: print("epub creation failed") print(str(e)) epub_filepath = "" pass # **always** restore the repo working directory HEAD before leaving checkout_head(repo_path) return epub_filepath
def test_simple(self): self.repo.refs["refs/tags/foo"] = "aa" * 20 self.repo.refs["refs/tags/bar/bla"] = "bb" * 20 tags = porcelain.list_tags(self.repo.path) self.assertEqual(["bar/bla", "foo"], tags)
def test_empty(self): tags = porcelain.list_tags(self.repo.path) self.assertEqual([], tags)
def performCommit(localRepo, bookid, bookinfo, bookroot, bookfiles): has_error = False staged = [] added = [] ignored = [] # convert url paths to os specific paths repo_home = pathof(localRepo) repo_home = repo_home.replace("/", os.sep) repo_path = os.path.join(repo_home, "epub_" + bookid) book_home = pathof(bookroot) book_home = book_home.replace("/", os.sep) # convert from bookpaths to os relative file paths filepaths = [] for bkpath in bookfiles: afile = pathof(bkpath) afile = afile.replace("/", os.sep) filepaths.append(afile) cdir = os.getcwd() if os.path.exists(repo_path): # handle updating the staged files and commiting and tagging # first collect info to determine files to delete form repo # current tag, etc os.chdir(repo_path) # determine the new tag tags = porcelain.list_tags(repo='.') tagname = "V%04d" % (len(tags) + 1) tagmessage = "Tag: " + tagname message = "updating to " + tagname # extra parameters must be passed as bytes if annotated is true tagname = utf8_str(tagname) message = utf8_str(message) tagmessage = utf8_str(tagmessage) # delete files that are no longer needed from staging area tracked = [] tracked = porcelain.ls_files(repo='.') files_to_delete = [] for afile in tracked: afile = pathof(afile) if afile not in filepaths: if afile not in ["mimetype", ".gitignore", ".bookinfo"]: files_to_delete.append(afile) if len(files_to_delete) > 0: porcelain.rm(repo='.', paths=files_to_delete) # copy over current files copy_book_contents_to_destination(book_home, filepaths, repo_path) (staged, unstaged, untracked) = porcelain.status(repo='.') files_to_update = [] for afile in unstaged: afile = pathof(afile) files_to_update.append(afile) for afile in untracked: afile = pathof(afile) files_to_update.append(afile) (added, ignored) = porcelain.add(repo='.', paths=files_to_update) commit_sha1 = porcelain.commit(repo='.', message=message, author=_SIGIL, committer=_SIGIL) # create annotated tags so we can get a date history tag = porcelain.tag_create(repo='.', tag=tagname, message=tagmessage, annotated=True, author=_SIGIL) os.chdir(cdir) add_bookinfo(repo_path, bookinfo, bookid, unicode_str(tagname)) else: # this will be an initial commit to this repo tagname = b"V0001" tagmessage = b'First Tag' message = b"Initial Commit" os.makedirs(repo_path) add_gitignore(repo_path) add_gitattributes(repo_path) cdir = os.getcwd() os.chdir(repo_path) r = porcelain.init(path='.', bare=False) staged = copy_book_contents_to_destination(book_home, filepaths, repo_path) (added, ignored) = porcelain.add(repo='.', paths=staged) # it seems author, committer, messages, and tagname only work with bytes if annotated=True commit_sha1 = porcelain.commit(repo='.', message=message, author=_SIGIL, committer=_SIGIL) tag = porcelain.tag_create(repo='.', tag=tagname, message=tagmessage, annotated=True, author=_SIGIL) os.chdir(cdir) add_bookinfo(repo_path, bookinfo, bookid, unicode_str(tagname)) result = "\n".join(added) result = result + "***********" + "\n".join(ignored) if not has_error: return result return ''
def test_simple(self): self.repo.refs["refs/tags/foo"] = "aa" * 20 self.repo.refs["refs/tags/bar/bla"] = "bb" * 20 tags = porcelain.list_tags(self.repo.path) self.assertEquals(["bar/bla", "foo"], tags)
def test_empty(self): tags = porcelain.list_tags(self.repo.path) self.assertEquals([], tags)