def test_add_file(self): f = open(os.path.join(self.repo.path, 'foo'), 'w') try: f.write("BAR") finally: f.close() porcelain.add(self.repo.path, paths=["foo"])
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) target_repo.close() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') # Pull changes into the cloned repo porcelain.pull(target_path, self.repo.path, b'refs/heads/master', outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with closing(Repo(target_path)) as r: self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
def test_receive_pack(self): filename = "foo" with open(os.path.join(self.repo.path, filename), "w") as f: f.write("stuff") porcelain.add(repo=self.repo.path, paths=filename) self.repo.do_commit( message=b"test status", author=b"", committer=b"", author_timestamp=1402354300, commit_timestamp=1402354300, author_timezone=0, commit_timezone=0, ) outf = BytesIO() exitcode = porcelain.receive_pack(self.repo.path, BytesIO(b"0000"), outf) outlines = outf.getvalue().splitlines() self.assertEqual( [ b"00739e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00 report-status " b"delete-refs quiet ofs-delta side-band-64k no-done", b"003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master", b"0000", ], outlines, ) self.assertEqual(0, exitcode)
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test', author='test', committer='test') # Setup target repo target_path = tempfile.mkdtemp() porcelain.clone(self.repo.path, target=target_path, outstream=outstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test2', author='test2', committer='test2') # Pull changes into the cloned repo porcelain.pull(target_path, self.repo.path, 'refs/heads/master', outstream=outstream, errstream=errstream) # Check the target repo for pushed changes r = Repo(target_path) self.assertEqual(r['HEAD'].id, self.repo['HEAD'].id)
def test_add_default_paths(self): # create a file for initial commit fullpath = os.path.join(self.repo.path, 'blah') with open(fullpath, 'w') as f: f.write("\n") porcelain.add(repo=self.repo.path, paths=[fullpath]) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') # Add a second test file and a file in a directory with open(os.path.join(self.repo.path, 'foo'), 'w') as f: f.write("\n") os.mkdir(os.path.join(self.repo.path, 'adir')) with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f: f.write("\n") cwd = os.getcwd() try: os.chdir(self.repo.path) porcelain.add(self.repo.path) finally: os.chdir(cwd) # Check that foo was added and nothing in .git was modified index = self.repo.open_index() self.assertEqual(sorted(index), [b'adir/afile', b'blah', b'foo'])
def test_status(self): """Integration test for `status` functionality.""" # Commit a dummy file then modify it fullpath = os.path.join(self.repo.path, "foo") with open(fullpath, "w") as f: f.write("origstuff") porcelain.add(repo=self.repo.path, paths=["foo"]) porcelain.commit(repo=self.repo.path, message=b"test status", author=b"", committer=b"") # modify access and modify time of path os.utime(fullpath, (0, 0)) with open(fullpath, "wb") as f: f.write(b"stuff") # Make a dummy file and stage it filename_add = "bar" fullpath = os.path.join(self.repo.path, filename_add) with open(fullpath, "w") as f: f.write("stuff") porcelain.add(repo=self.repo.path, paths=filename_add) results = porcelain.status(self.repo) self.assertEqual(results.staged["add"][0], filename_add.encode("ascii")) self.assertEqual(results.unstaged, [b"foo"])
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b"test", author=b"test", committer=b"test") # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b"test2", author=b"test2", committer=b"test2") self.assertFalse(self.repo[b"HEAD"].id in target_repo) target_repo.close() # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with closing(Repo(target_path)) as r: self.assertTrue(self.repo[b"HEAD"].id in r)
def test_hard_commit(self): with open(os.path.join(self.repo.path, "foo"), "w") as f: f.write("BAR") porcelain.add(self.repo.path, paths=["foo"]) sha = porcelain.commit( self.repo.path, message=b"Some message", committer=b"Jane <*****@*****.**>", author=b"John <*****@*****.**>", ) with open(os.path.join(self.repo.path, "foo"), "wb") as f: f.write(b"BAZ") porcelain.add(self.repo.path, paths=["foo"]) porcelain.commit( self.repo.path, message=b"Some other message", committer=b"Jane <*****@*****.**>", author=b"John <*****@*****.**>", ) porcelain.reset(self.repo, "hard", sha) index = self.repo.open_index() changes = list(tree_changes(self.repo, index.commit(self.repo.object_store), self.repo[sha].tree)) self.assertEqual([], changes)
def test_status(self): """Integration test for `status` functionality.""" # Commit a dummy file then modify it fullpath = os.path.join(self.repo.path, 'foo') with open(fullpath, 'w') as f: f.write('origstuff') porcelain.add(repo=self.repo.path, paths=['foo']) porcelain.commit(repo=self.repo.path, message=b'test status', author=b'', committer=b'') # modify access and modify time of path os.utime(fullpath, (0, 0)) with open(fullpath, 'wb') as f: f.write(b'stuff') # Make a dummy file and stage it filename_add = 'bar' fullpath = os.path.join(self.repo.path, filename_add) with open(fullpath, 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename_add) results = porcelain.status(self.repo) self.assertEqual(results.staged['add'][0], filename_add.encode('ascii')) self.assertEqual(results.unstaged, [b'foo'])
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') self.assertFalse(self.repo[b'HEAD'].id in target_repo) # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes r = Repo(target_path) self.assertTrue(self.repo[b'HEAD'].id in r)
def test_hard_head(self): f = open(os.path.join(self.repo.path, 'foo'), 'w') try: f.write("BAR") finally: f.close() porcelain.add(self.repo.path, paths=["foo"]) porcelain.commit(self.repo.path, message="Some message", committer="Jane <*****@*****.**>", author="John <*****@*****.**>") f = open(os.path.join(self.repo.path, 'foo'), 'w') try: f.write("OOH") finally: f.close() porcelain.reset(self.repo, "hard", "HEAD") index = self.repo.open_index() changes = list(tree_changes(self.repo, index.commit(self.repo.object_store), self.repo['HEAD'].tree)) self.assertEqual([], changes)
def setUp(self): super(PullTests, self).setUp() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') # Setup target repo self.target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.target_path) target_repo = porcelain.clone(self.repo.path, target=self.target_path, errstream=BytesIO()) target_repo.close() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2 <email>', committer=b'test2 <email>') self.assertTrue(b'refs/heads/master' in self.repo.refs) self.assertTrue(b'refs/heads/master' in target_repo.refs)
def rename_file(self, old_path, new_path): # paths must not be unicode. :( old_files = [old_path] new_files = [new_path] if self.save_script and ".ipynb" in old_path: old_files.append(old_files[0].replace('.ipynb', '.py')) new_files.append(new_files[0].replace('.ipynb', '.py')) renamed = super(GitNotebookManager, self).rename_file(old_path, new_path) isFolder = os.path.splitext(old_path)[-1] == "" if any(old_path.endswith(ext) for ext in self._tracked_ext) or isFolder: if isFolder: subprocess.call(["git", "rm", "-r", str(old_path)], shell=False) subprocess.call(["git", "add", str(new_path)], shell=False) self._repo = None self._check_repo() else: git.rm(self._repo, [str(_) for _ in old_files]) git.add(self._repo, [str(_) for _ in new_files]) self.log.debug("Notebook renamed from '%s' to '%s'" % (old_files[0], new_files[0])) git.commit(self._repo, "IPython notebook rename\n\n" "Automated commit from IPython via ipylogue", committer=self.committer_fullname) # git.push(self._repo, self._repo.get_config()[('remote', 'origin')]["url"], "refs/heads/master") return renamed
def git_add(args): if len(args) > 0: repo = _get_repo() args = [os.path.join(os.path.relpath('.', repo.path),x) for x in args] #repo.stage(args) porcelain.add(repo.repo, args) else: print command_help['add']
def add(self, a): #a can be list of files or a single file print self.repo_name print self.repo if type(a) == list: for i in a: p.add(self.repo, i) else: p.add(self.repo, a)
def test_simple(self): """ Basic test of porcelain push where self.repo is the remote. First clone the remote, commit a file to the clone, then push the changes back to the remote. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'author <email>', committer=b'committer <email>') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, clone_path) target_repo = porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) try: self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD']) finally: target_repo.close() # create a second file to be pushed back to origin handle, fullpath = tempfile.mkstemp(dir=clone_path) os.close(handle) porcelain.add(repo=clone_path, paths=[fullpath]) porcelain.commit(repo=clone_path, message=b'push', author=b'author <email>', committer=b'committer <email>') # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" new_id = self.repo[b'HEAD'].id self.assertNotEqual(new_id, ZERO_SHA) self.repo.refs[refs_path] = new_id # Push to the remote porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path, outstream=outstream, errstream=errstream) # Check that the target and source with Repo(clone_path) as r_clone: self.assertEqual({ b'HEAD': new_id, b'refs/heads/foo': r_clone[b'HEAD'].id, b'refs/heads/master': new_id, }, self.repo.get_refs()) self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id) # Get the change in the target repo corresponding to the add # this will be in the foo branch. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree, self.repo[b'refs/heads/foo'].tree))[0] self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
def test_remove_file_staged(self): fullpath = os.path.join(self.repo.path, 'foo') with open(fullpath, 'w') as f: f.write("BAR") cwd = os.getcwd() try: os.chdir(self.repo.path) porcelain.add(self.repo.path, paths=[fullpath]) self.assertRaises(Exception, porcelain.rm, self.repo.path, paths=["foo"]) finally: os.chdir(cwd)
def test_simple(self): # Commit a dummy file then modify it fullpath = os.path.join(self.repo.path, "foo") with open(fullpath, "w") as f: f.write("origstuff") porcelain.add(repo=self.repo.path, paths=["foo"]) porcelain.commit(repo=self.repo.path, message=b"test status", author=b"", committer=b"") f = StringIO() porcelain.ls_tree(self.repo, b"HEAD", outstream=f) self.assertEqual(f.getvalue(), "100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n")
def git_add(args): if len(args) > 0: repo = _get_repo() cwd = os.getcwd() args = [os.path.join(os.path.relpath(cwd, repo.path), x) if not os.path.samefile(cwd, repo.path) else x for x in args] for file in args: print 'Adding {0}'.format(file) #repo.stage(args) porcelain.add(repo.repo, args) else: print command_help['add']
def test_simple(self): # Commit a dummy file then modify it fullpath = os.path.join(self.repo.path, 'foo') with open(fullpath, 'w') as f: f.write('origstuff') porcelain.add(repo=self.repo.path, paths=['foo']) porcelain.commit(repo=self.repo.path, message=b'test status', author=b'', committer=b'') f = StringIO() porcelain.ls_tree(self.repo, b"HEAD", outstream=f) self.assertEqual( f.getvalue(), '100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n')
def test_remove_file(self): fullpath = os.path.join(self.repo.path, 'foo') with open(fullpath, 'w') as f: f.write("BAR") porcelain.add(self.repo.path, paths=[fullpath]) porcelain.commit(repo=self.repo, message=b'test', author=b'test <email>', committer=b'test <email>') self.assertTrue(os.path.exists(os.path.join(self.repo.path, 'foo'))) cwd = os.getcwd() try: os.chdir(self.repo.path) porcelain.remove(self.repo.path, paths=["foo"]) finally: os.chdir(cwd) self.assertFalse(os.path.exists(os.path.join(self.repo.path, 'foo')))
def git_add(args): if len(args) > 0: repo = _get_repo() cwd = os.getcwd() args = [os.path.join(os.path.relpath(cwd, repo.path), x) if not os.path.samefile(cwd, repo.path) else x for x in args] for file in args: if os.path.exists(file): print 'Adding {0}'.format(file) porcelain.add(repo.repo.path, [file]) else: print '{} does not exist. skipping'.format(file) else: print command_help['add']
def test_get_tree_changes_delete(self): """Unit test for get_tree_changes delete.""" # Make a dummy file, stage, commit, remove filename = "foo" with open(os.path.join(self.repo.path, filename), "w") as f: f.write("stuff") porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b"test status", author=b"", committer=b"") porcelain.rm(repo=self.repo.path, paths=[filename]) changes = porcelain.get_tree_changes(self.repo.path) self.assertEqual(changes["delete"][0], filename.encode("ascii")) self.assertEqual(len(changes["add"]), 0) self.assertEqual(len(changes["modify"]), 0) self.assertEqual(len(changes["delete"]), 1)
def test_with_remote_name(self): remote_name = b'origin' outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # Capture current refs target_refs = target_repo.get_refs() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2 <email>', committer=b'test2 <email>') self.assertFalse(self.repo[b'HEAD'].id in target_repo) target_repo.close() # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, remote_name=remote_name, outstream=outstream, errstream=errstream) # Assert that fetch updated the local image of the remote self.assert_correct_remote_refs( target_repo.get_refs(), self.repo.get_refs()) # Check the target repo for pushed changes, as well as updates # for the refs with Repo(target_path) as r: self.assertTrue(self.repo[b'HEAD'].id in r) self.assertNotEqual(self.repo.get_refs(), target_refs)
def test_get_tree_changes_delete(self): """Unit test for get_tree_changes delete.""" # Make a dummy file, stage, commit, remove filename = 'foo' with open(os.path.join(self.repo.path, filename), 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test status', author=b'', committer=b'') porcelain.rm(repo=self.repo.path, paths=[filename]) changes = porcelain.get_tree_changes(self.repo.path) self.assertEqual(changes['delete'][0], filename.encode('ascii')) self.assertEqual(len(changes['add']), 0) self.assertEqual(len(changes['modify']), 0) self.assertEqual(len(changes['delete']), 1)
def test_receive_pack(self): filename = 'foo' with open(os.path.join(self.repo.path, filename), 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) self.repo.do_commit(message='test status', author='', committer='', author_timestamp=1402354300, commit_timestamp=1402354300, author_timezone=0, commit_timezone=0) outf = BytesIO() exitcode = porcelain.receive_pack(self.repo.path, BytesIO("0000"), outf) outlines = outf.getvalue().splitlines() self.assertEqual([ '005a9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00report-status ' 'delete-refs side-band-64k', '003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master', '0000'], outlines) self.assertEqual(0, exitcode)
def test_add_default_paths(self): # create a file for initial commit with open(os.path.join(self.repo.path, "blah"), "w") as f: f.write("\n") porcelain.add(repo=self.repo.path, paths=["blah"]) porcelain.commit(repo=self.repo.path, message=b"test", author=b"test", committer=b"test") # Add a second test file and a file in a directory with open(os.path.join(self.repo.path, "foo"), "w") as f: f.write("\n") os.mkdir(os.path.join(self.repo.path, "adir")) with open(os.path.join(self.repo.path, "adir", "afile"), "w") as f: f.write("\n") porcelain.add(self.repo.path) # Check that foo was added and nothing in .git was modified index = self.repo.open_index() self.assertEqual(sorted(index), [b"adir/afile", b"blah", b"foo"])
def test_add_default_paths_subdir(self): os.mkdir(os.path.join(self.repo.path, 'foo')) with open(os.path.join(self.repo.path, 'blah'), 'w') as f: f.write("\n") with open(os.path.join(self.repo.path, 'foo', 'blie'), 'w') as f: f.write("\n") cwd = os.getcwd() try: os.chdir(os.path.join(self.repo.path, 'foo')) porcelain.add(repo=self.repo.path) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') finally: os.chdir(cwd) index = self.repo.open_index() self.assertEqual(sorted(index), [b'foo/blie'])
def test_simple(self): """ Basic test of porcelain push where self.repo is the remote. First clone the remote, commit a file to the clone, then push the changes back to the remote. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'', committer=b'') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() target_repo = porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) target_repo.close() # create a second file to be pushed back to origin handle, fullpath = tempfile.mkstemp(dir=clone_path) os.close(handle) porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)]) porcelain.commit(repo=clone_path, message=b'push', author=b'', committer=b'') # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" self.repo[refs_path] = self.repo[b'HEAD'] # Push to the remote porcelain.push(clone_path, self.repo.path, refs_path, outstream=outstream, errstream=errstream) # Check that the target and source with closing(Repo(clone_path)) as r_clone: # Get the change in the target repo corresponding to the add # this will be in the foo branch. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree, self.repo[b'refs/heads/foo'].tree))[0] self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id) self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
def test_get_tree_changes_modify(self): """Unit test for get_tree_changes modify.""" # Make a dummy file, stage, commit, modify filename = 'foo' fullpath = os.path.join(self.repo.path, filename) with open(fullpath, 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test status', author='', committer='') with open(fullpath, 'w') as f: f.write('otherstuff') porcelain.add(repo=self.repo.path, paths=filename) changes = porcelain.get_tree_changes(self.repo.path) self.assertEqual(changes['modify'][0], filename) self.assertEqual(len(changes['add']), 0) self.assertEqual(len(changes['modify']), 1) self.assertEqual(len(changes['delete']), 0)
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test', author='test', committer='test') # Setup target repo target_path = tempfile.mkdtemp() target_repo = porcelain.clone(self.repo.path, target=target_path, outstream=outstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test2', author='test2', committer='test2') self.assertFalse(self.repo['HEAD'].id in target_repo) # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes r = Repo(target_path) self.assertTrue(self.repo['HEAD'].id in r)
def test_add_default_paths(self): # create a file for initial commit with open(os.path.join(self.repo.path, 'blah'), 'w') as f: f.write("\n") porcelain.add(repo=self.repo.path, paths=['blah']) porcelain.commit(repo=self.repo.path, message='test', author='test', committer='test') # Add a second test file and a file in a directory with open(os.path.join(self.repo.path, 'foo'), 'w') as f: f.write("\n") os.mkdir(os.path.join(self.repo.path, 'adir')) with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f: f.write("\n") porcelain.add(self.repo.path) # Check that foo was added and nothing in .git was modified index = self.repo.open_index() self.assertEquals(list(index), ['blah', 'foo', 'adir/afile'])
def test_get_tree_changes_modify(self): """Unit test for get_tree_changes modify.""" # Make a dummy file, stage, commit, modify filename = 'foo' fullpath = os.path.join(self.repo.path, filename) with open(fullpath, 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test status', author=b'', committer=b'') with open(fullpath, 'w') as f: f.write('otherstuff') porcelain.add(repo=self.repo.path, paths=filename) changes = porcelain.get_tree_changes(self.repo.path) self.assertEqual(changes['modify'][0], filename.encode('ascii')) self.assertEqual(len(changes['add']), 0) self.assertEqual(len(changes['modify']), 1) self.assertEqual(len(changes['delete']), 0)
def test_add_ignored(self): with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f: f.write("foo") with open(os.path.join(self.repo.path, 'foo'), 'w') as f: f.write("BAR") with open(os.path.join(self.repo.path, 'bar'), 'w') as f: f.write("BAR") (added, ignored) = porcelain.add(self.repo.path, paths=[ os.path.join(self.repo.path, "foo"), os.path.join(self.repo.path, "bar")]) self.assertIn(b"bar", self.repo.open_index()) self.assertEqual(set(['bar']), set(added)) self.assertEqual(set(['foo']), ignored)
def test_receive_pack(self): filename = 'foo' fullpath = os.path.join(self.repo.path, filename) with open(fullpath, 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=fullpath) self.repo.do_commit(message=b'test status', author=b'author <email>', committer=b'committer <email>', author_timestamp=1402354300, commit_timestamp=1402354300, author_timezone=0, commit_timezone=0) outf = BytesIO() exitcode = porcelain.receive_pack( self.repo.path, BytesIO(b"0000"), outf) outlines = outf.getvalue().splitlines() self.assertEqual([ b'0091319b56ce3aee2d489f759736a79cc552c9bb86d9 HEAD\x00 report-status ' # noqa: E501 b'delete-refs quiet ofs-delta side-band-64k ' b'no-done symref=HEAD:refs/heads/master', b'003f319b56ce3aee2d489f759736a79cc552c9bb86d9 refs/heads/master', b'0000'], outlines) self.assertEqual(0, exitcode)
def test_get_tree_changes_add(self): """Unit test for get_tree_changes add.""" # Make a dummy file, stage filename = 'bar' with open(os.path.join(self.repo.path, filename), 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test status', author='', committer='') filename = 'foo' with open(os.path.join(self.repo.path, filename), 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) changes = porcelain.get_tree_changes(self.repo.path) self.assertEqual(changes['add'][0], filename) self.assertEqual(len(changes['add']), 1) self.assertEqual(len(changes['modify']), 0) self.assertEqual(len(changes['delete']), 0)
def test_receive_pack(self): filename = 'foo' with open(os.path.join(self.repo.path, filename), 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=filename) self.repo.do_commit(message=b'test status', author=b'', committer=b'', author_timestamp=1402354300, commit_timestamp=1402354300, author_timezone=0, commit_timezone=0) outf = BytesIO() exitcode = porcelain.receive_pack(self.repo.path, BytesIO(b"0000"), outf) outlines = outf.getvalue().splitlines() self.assertEqual([ b'005a9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00report-status ' b'delete-refs side-band-64k', b'003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master', b'0000' ], outlines) self.assertEqual(0, exitcode)
def test_hard_commit(self): with open(os.path.join(self.repo.path, 'foo'), 'w') as f: f.write("BAR") porcelain.add(self.repo.path, paths=["foo"]) sha = porcelain.commit(self.repo.path, message=b"Some message", committer=b"Jane <*****@*****.**>", author=b"John <*****@*****.**>") with open(os.path.join(self.repo.path, 'foo'), 'wb') as f: f.write(b"BAZ") porcelain.add(self.repo.path, paths=["foo"]) porcelain.commit(self.repo.path, message=b"Some other message", committer=b"Jane <*****@*****.**>", author=b"John <*****@*****.**>") porcelain.reset(self.repo, "hard", sha) index = self.repo.open_index() changes = list(tree_changes(self.repo, index.commit(self.repo.object_store), self.repo[sha].tree)) self.assertEqual([], changes)
def upload(self, cmd: str, meta: dict): """Push the current state of the registry to Git.""" index = os.path.join(self.cached_repo, self.INDEX_FILE) if os.path.exists(index): os.remove(index) self._log.info("Writing the new index.json ...") with open(index, "w") as _out: json.dump(self.contents, _out) git.add(self.cached_repo, [index]) message = self.COMMIT_MESSAGES[cmd].format(**meta) if self.signoff: global_conf_path = os.path.expanduser("~/.gitconfig") if os.path.exists(global_conf_path): with open(global_conf_path, "br") as _in: conf = ConfigFile.from_file(_in) try: name = conf.get(b"user", b"name").decode() email = conf.get(b"user", b"email").decode() message += self.DCO_MESSAGE.format(name=name, email=email) except KeyError: self._log.warning( "Did not find name or email in %s, committing without DCO.", global_conf_path) else: self._log.warning( "Global git configuration file %s does not exist, " "committing without DCO.", global_conf_path) else: self._log.info("Committing the index without DCO.") git.commit(self.cached_repo, message=message) self._log.info("Pushing the updated index ...") # TODO: change when https://github.com/dulwich/dulwich/issues/631 gets addressed git.push(self.cached_repo, self.remote_url, b"master") if self._are_local_and_remote_heads_different(): self._log.error("Push has failed") raise ValueError("Push has failed")
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) target_repo.close() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') self.assertTrue(b'refs/heads/master' in self.repo.refs) self.assertTrue(b'refs/heads/master' in target_repo.refs) # Pull changes into the cloned repo porcelain.pull(target_path, self.repo.path, b'refs/heads/master', outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with closing(Repo(target_path)) as r: self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
def example(): print '=== Create folders and blobs ===' os.mkdir('git_test') repo = Repo.init('git_test') for x in range(5): r = Repo('git_test') s = Snapshot(r) root = s.root() print '$ ls' print root.nodes() dir1 = root.add_treenode('dir1') blob = dir1.add_blobnode('file1', 'content%s' % x) blob.save() dir2 = dir1.add_treenode('dir2') blob = dir2.add_blobnode('file2', 'content%s' % x) blob.save() dir2.save() dir1.save() root.save() print 'commit:', s.commit('testuser', message='test commit: %s' % x).id print '$ cat dir1/file1' print root.get_node('dir1').get_node('file1').read_data() print dir1.get_node('dir2').get_node('file2').read_data() print #Using Porcelain #Initiating a new repo repo = porcelain.init("myrepo") #cloning a repo from a server porcelain.clone("https://github.com/sugarlabs/browse-activity.git", "browse_activity_clone") #local cloning porcelain.clone("/home/vikram/browse_activity_clone", "browse_activity_clone_1") print print "Commiting" open("myrepo/testfile", "w").write("data") porcelain.add(repo, "myrepo/testfile") porcelain.commit(repo, "A sample commit") open("myrepo/testfile", "w").write("data1") porcelain.add(repo, "myrepo/testfile") porcelain.commit(repo, "Second commit") open("myrepo/testfile1", "w").write("sugar-labs") porcelain.add(repo, "myrepo/testfile1") porcelain.commit(repo, "First commit") print "Commit Logs:" print porcelain.log(repo)
def _copy(self, name=None, copy_name=None): if not name: self.q.put([ Action.ask_input, _("Which password do you want to copy?"), "", "copy" ]) elif not copy_name: self.q.put([ Action.ask_input, _("What should the copy of {} be named?").format(name), name, "copy {}".format(name) ]) else: try: original_file_path = os.path.join(self._get_data_location(), "{}.gpg".format(name)) copy_file_path = os.path.join(self._get_data_location(), "{}.gpg".format(copy_name)) shutil.copyfile(original_file_path, copy_file_path) if self.git_repo: porcelain.add(self.git_repo, [copy_file_path]) porcelain.commit( self.git_repo, message="Copied {} to {} with Pext".format( name, copy_name)) self._git_push() except shutil.SameFileError: self.q.put([ Action.ask_input, _("What should the copy of {} be named?").format(name), name, "copy {}".format(name) ]) return self.q.put([Action.set_selection, []])
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2 <email>', committer=b'test2 <email>') self.assertFalse(self.repo[b'HEAD'].id in target_repo) target_repo.close() # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with Repo(target_path) as r: self.assertTrue(self.repo[b'HEAD'].id in r)
def test_get_tree_changes_delete(self): """Unit test for get_tree_changes delete.""" # Make a dummy file, stage, commit, remove filename = 'foo' fullpath = os.path.join(self.repo.path, filename) with open(fullpath, 'w') as f: f.write('stuff') porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test status', author=b'author <email>', committer=b'committer <email>') cwd = os.getcwd() try: os.chdir(self.repo.path) porcelain.remove(repo=self.repo.path, paths=[filename]) finally: os.chdir(cwd) changes = porcelain.get_tree_changes(self.repo.path) self.assertEqual(changes['delete'][0], filename.encode('ascii')) self.assertEqual(len(changes['add']), 0) self.assertEqual(len(changes['modify']), 0) self.assertEqual(len(changes['delete']), 1)
def commit_and_push(version: str, repository: Repository) -> None: """ Commit and push all changes. """ local_repository = Repo('.') paths = ['CHANGELOG.rst'] _, ignored = add(paths=paths) assert not ignored message = b'Update for release ' + version.encode('utf-8') commit(message=message) branch_name = 'master' push( repo=local_repository, remote_location=repository.ssh_url, refspecs=branch_name.encode('utf-8'), )
def commit_and_push(version: str) -> None: """ Commit and push all changes. """ repo = Repo('.') paths = ['dcosdocker.rb', 'CHANGELOG.rst', 'vagrant/Vagrantfile'] _, ignored = add(paths=paths) assert not ignored message = b'Update for release ' + version.encode('utf-8') commit(message=message) branch_name = 'master' push( repo=repo, remote_location='[email protected]:mesosphere/dcos-e2e.git', refspecs=branch_name.encode('utf-8'), )
def test_simple(self): """ Basic test of porcelain push where self.repo is the remote. First clone the remote, commit a file to the clone, then push the changes back to the remote. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'author <email>', committer=b'committer <email>') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, clone_path) target_repo = porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) try: self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD']) finally: target_repo.close() # create a second file to be pushed back to origin handle, fullpath = tempfile.mkstemp(dir=clone_path) os.close(handle) porcelain.add(repo=clone_path, paths=[fullpath]) porcelain.commit(repo=clone_path, message=b'push', author=b'author <email>', committer=b'committer <email>') # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" new_id = self.repo[b'HEAD'].id self.assertNotEqual(new_id, ZERO_SHA) self.repo.refs[refs_path] = new_id # Push to the remote porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path, outstream=outstream, errstream=errstream) # Check that the target and source with Repo(clone_path) as r_clone: self.assertEqual( { b'HEAD': new_id, b'refs/heads/foo': r_clone[b'HEAD'].id, b'refs/heads/master': new_id, }, self.repo.get_refs()) self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id) # Get the change in the target repo corresponding to the add # this will be in the foo branch. change = list( tree_changes(self.repo, self.repo[b'HEAD'].tree, self.repo[b'refs/heads/foo'].tree))[0] self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
import shutil from dulwich import porcelain # make sure we're starting fresh shutil.rmtree("demo_repo", ignore_errors=True) repo = porcelain.init("demo_repo") # create an initial commit, with our content. with open("demo_repo/a_file.txt", "wb") as f: f.write(b"Blobfish are people too\n") porcelain.add(repo, "demo_repo/a_file.txt") # note path! porcelain.commit(repo, b"The beginning", author=b"meejah <*****@*****.**>") # make a single change with open("demo_repo/a_file.txt", "wb") as f: f.write(b"Blobfish are fish, not people\n") porcelain.add(repo, "demo_repo/a_file.txt") porcelain.commit( repo, b"blobfish are aquatic animals", author=b"meejah <*****@*****.**>", )
def test_remove_file(self): with open(os.path.join(self.repo.path, 'foo'), 'w') as f: f.write("BAR") porcelain.add(self.repo.path, paths=["foo"]) porcelain.rm(self.repo.path, paths=["foo"])
def add(ctx, paths): if '.' in paths: click.echo('Warning: adding "." does not work yet') result = gitp.add(ctx.obj['repo'], list(paths)) click.echo(result)
def run(self, argv): parser = argparse.ArgumentParser() args = parser.parse_args(argv) porcelain.add(".", paths=args)
def add(self, workspace, path, **kw): if workspace.working_dir in path: path = path.replace(workspace.working_dir + os.sep, '') porcelain.add(repo=workspace.working_dir, paths=[path])
def run(self, args): opts, args = getopt(args, "", []) porcelain.add(".", paths=args)
def merge(args): helptext='''git merge' [--msg <msg>] [<commit>] git merge --abort\n merges <commit> into HEAD, or remote tracking branch if commit not specified. <commit> can be a local or remote ref, or an existing commit sha. merge will handle unambiguous conflicts between head and other merge head, and will insert conflict markers if conflicts cannot be resolved. note that the strategy used will prefer changes in the local head. for instance, if HEAD deleted a section, while MERGE_HEAD modified the same action, the section will be deleted from the final without indicating a conflict. be sure to commit any local changes before running merge, as files in working tree (i.e on disk) are changed, and checked in, which will probably overwrite any local uncomitted changes. note merge will not actually commit anything. run git commit to commit a successful merge. --abort will remove the MERGE_HEAD and MERGE_MSG files, and will reset staging area, but wont affect files on disk. use git reset --hard or git checkout if this is desired. ''' repo=_get_repo() print '_'*30 parser=argparse.ArgumentParser(prog='merge', usage=helptext) parser.add_argument('commit',action='store',nargs='?', help='commit sha, local branch, or remote branch name to merge from') parser.add_argument('--msg',nargs=1,action='store',help='commit message to store') parser.add_argument('--abort',action='store_true',help='abort in progress merge attempt') result=parser.parse_args(args) if result.abort: print 'attempting to undo merge. beware, files in working tree are not touched. \nused git reset --hard to revert particular files' git_reset([]) os.remove(os.path.join(repo.repo.controldir(),'MERGE_HEAD')) os.remove(os.path.join(repo.repo.controldir(),'MERGE_MSG')) #todo: check for uncommitted changes and confirm # first, determine merge head merge_head = find_revision_sha(repo,result.commit or get_remote_tracking_branch(repo,repo.active_branch)) if not merge_head: raise GitError('must specify a commit sha, branch, remote tracking branch to merge from. or, need to set-upstream branch using git branch --set-upstream <remote>[/<branch>]') head=find_revision_sha(repo,repo.active_branch) base_sha=merge_base(repo,head,merge_head)[0] #fixme, what if multiple bases if base_sha==head: print 'Fast forwarding {} to {}'.format(repo.active_branch,merge_head) repo.refs['HEAD']=merge_head return if base_sha == merge_head: print 'head is already up to date' return print 'merging <{}> into <{}>\n{} commits ahead of merge base <{}> respectively'.format(merge_head[0:7],head[0:7],count_commits_between(repo,merge_head,head),base_sha[0:7]) base_tree=repo[base_sha].tree merge_head_tree=repo[merge_head].tree head_tree=repo[head].tree num_conflicts,added,removed=merge_trees(repo.repo.object_store, base_tree,head_tree,merge_head_tree) # update index if added: porcelain.add(repo.path, added) if removed: porcelain.rm(repo.path, removed) repo.repo._put_named_file('MERGE_HEAD',merge_head) repo.repo._put_named_file('MERGE_MSG','Merged from {}({})'.format(merge_head, result.commit)) print 'Merge complete with {} conflicted files'.format(num_conflicts) print '''Merged files were added to the staging area, but have not yet been comitted.
def dbCompile(args): if not os.path.isdir(args.folder): print('Cannot find YAML files; please check folder location and try' ' again.') sys.exit(1) missing_ids = scan_ids(args) if missing_ids: print('Database has missing IDs. Continue anyway? [y/N]') reply = input("> ") if reply[:1].lower() != 'y': print('Run "{}" to fix problem.'.format(parser_checkids.prog)) sys.exit(0) if os.path.isfile(args.db): print('Database file already exists at {}.'.format(args.db)) print('Do you wish to replace it? [y/N]') reply = input("> ") if reply[:1].lower() != 'y': print('Okay. I will leave it alone.') sys.exit(0) elif not os.path.isdir(os.path.dirname(args.db)): os.makedirs(os.path.dirname(args.db)) isCompiled = False db = dict() for folder in subfolders: folder_path = os.path.join(args.folder, folder) if not os.path.isdir(folder_path): continue db[folder] = dict() for entry in os.listdir(folder_path): if (os.path.splitext(entry)[1] != '.yml'): continue with open(os.path.join(folder_path, entry), 'r') as r: record = yaml.safe_load(r) record['slug'] = os.path.splitext(entry)[0] id_list = list() for identifier in record['identifiers']: if identifier['scheme'] == 'RDA-MSCWG': id_string = identifier['id'] id_number = id_string[5:] else: id_list.append(identifier) if id_list: record['identifiers'] = id_list else: del record['identifiers'] if 'keywords' in record: # Deduplicate and sort term_set = set() for term in record['keywords']: if term: term_set.add(term) terms = list(term_set) terms.sort() record['keywords'] = terms db[folder][id_number] = record isCompiled = True if isCompiled: # Add blanks subfolder_lookup = {v: k for k, v in subfolders.items()} for mscid in missing_ids: folder = mscid[4:5] id_number = mscid[5:] db[subfolder_lookup.get(folder)][id_number] = dict() # Write the json file with open(args.db, 'w') as f: json.dump(db, f, default=json_serial, sort_keys=True, indent=2, ensure_ascii=False) # Add file to Git index try: repo = Repo(os.path.dirname(args.db)) except NotGitRepository: repo = Repo.init(os.path.dirname(args.db)) git.add(repo=os.path.dirname(args.db), paths=[args.db]) # Prepare commit information committer = 'MSCWG <{}>'.format(mscwg_email).encode('utf8') author = committer message = ('Refresh database fully from YAML files'.encode('utf8')) # Execute commit git.commit(os.path.dirname(args.db), message=message, author=author, committer=committer) else: print('No data files found, database not created.')
def test_add_file_absolute_path(self): # Absolute paths are (not yet) supported with open(os.path.join(self.repo.path, 'foo'), 'w') as f: f.write("BAR") porcelain.add(self.repo, paths=[os.path.join(self.repo.path, "foo")]) self.assertIn(b"foo", self.repo.open_index())
def test_add_file(self): fullpath = os.path.join(self.repo.path, 'foo') with open(fullpath, 'w') as f: f.write("BAR") porcelain.add(self.repo.path, paths=[fullpath]) self.assertIn(b"foo", self.repo.open_index())
print("Cloning...") repo = porcelain.clone( GITURL, password=TOKEN, # Tokens are kinda public keys, no need for a username but it still needs to be provided for Dulwich. username="******", target=gitdir, checkout=True, pool_manager=pool_manager, ) print("Cloned.") # Do something clever with the files in the repo, for instance create an empty readme. readme = gitdir / "readme.md" readme.touch() porcelain.add(repo, readme) print("Committing...") porcelain.commit(repo, "Empty readme added.") print("Commited.") print("Pushing...") porcelain.push( repo, remote_location=GITURL, refspecs="master", # branch to push to password=TOKEN, # Token are kinda public keys, no need for a username but it still needs to be provided for Dulwich. username="******", pool_manager=pool_manager, )
def test_simple(self): handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.repack(self.repo)
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 ''