def test_git_non_utf8(): """Test with non utf-8 encoding in changelog.""" working_tree = os.path.join(os.getcwd(), "working_tree") repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = os.path.join(working_tree, "new.txt") commit_msg = os.path.join(working_tree, "commit.txt") with open(commit_msg, "wb") as fd: fd.write(b"\x03\xff") with open(new_file, "wb") as fd: fd.write(b"\x03\xff") repo.git_cmd(["add", "new.txt"]) repo.git_cmd(["config", "user.email", "*****@*****.**"]) repo.git_cmd(["config", "user.name", "e3 core"]) repo.git_cmd(["commit", "-F", commit_msg]) with closing(tempfile.NamedTemporaryFile(mode="w", delete=False)) as fd: repo.write_log(fd) tmp_filename = fd.name try: with open(tmp_filename) as fd: commits = list(repo.parse_log(fd, max_diff_size=1024)) finally: rm(tmp_filename) assert "\\x03\\xff" in commits[0]["diff"]
def test_git_repo(): working_tree = unixpath(os.path.join(os.getcwd(), 'working_tree')) working_tree2 = os.path.join(os.getcwd(), 'working_tree2') repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = unixpath(os.path.join(working_tree, 'new.txt')) echo_to_file(new_file, 'new\n') repo.git_cmd(['add', 'new.txt']) repo.git_cmd(['config', 'user.email', '*****@*****.**']) repo.git_cmd(['config', 'user.name', 'e3 core']) repo.git_cmd(['commit', '-m', 'new file']) repo.git_cmd(['tag', '-a', '-m', 'new tag', '20.0855369232']) with open('log.txt', 'w') as f: repo.write_log(f) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert 'new file' in commits[0]['message'] assert commits[0]['email'] == '*****@*****.**' new_sha = commits[0]['sha'] assert '20.0855369232' in repo.describe() assert new_sha == repo.rev_parse() with pytest.raises(GitError) as err: repo.describe('g') assert 'describe --always g' in str(err) echo_to_file(new_file, 'new line\n', append=True) with open('commit1.diff', 'wb') as f: repo.write_local_diff(f) with open('commit1.diff', 'rb') as f: assert b'+new line' in f.read() echo_to_file(new_file, 10000 * '*') repo.git_cmd(['commit', '-a', '-m', 'file update']) with open('log2.txt', 'w') as f: repo.write_log(f) with open('log2.txt', 'r') as f: commits = list(repo.parse_log(f, max_diff_size=1000)) # assert b'diff too long' not in commits[1]['diff'] assert 'file update' in commits[0]['message'] assert 'diff too long' in commits[0]['diff'] assert 'new file' in commits[1]['message'] assert commits[1]['sha'] != commits[0]['sha'] assert commits[1]['diff'] != commits[0]['diff'] repo2 = GitRepository(working_tree2) repo2.init(url=working_tree, remote='tree1') try: repo2.update(url=working_tree, refspec='master') except GitError: if sys.platform == 'win32': # some git versions on windows do not support that well # ignore this test for now??? pass else: repo2.rev_parse() == repo.rev_parse()
def update_git(self, url, revision): """Update working dir using a Git repository. :param url: git repository url :type url: str :param revision: git revision :type revision: str """ # For git repositories revision cannot be None if revision is None: return ReturnValue.failure, None, None g = GitRepository(working_tree=self.working_dir) g.log_stream = e3.log.default_output_stream old_commit, new_commit = None, None # Create a remote for which name is bind to a an url # This ensure that when the url does not change, git will not # redownload all the objects on each objects (and thus avoid # disk space leaks). if isinstance(url, str): remote_name = hashlib.sha256(url.encode("utf-8")).hexdigest() else: remote_name = hashlib.sha256(url).hexdigest() g.init() # Do the remote addition manually as in that context we can ignore # safely any error returned by this command. try: remote_list = g.git_cmd(["remote"], output=PIPE).out.splitlines() if remote_name not in remote_list: g.git_cmd(["remote", "add", remote_name, url]) except Exception: # Ignore exception as it probably means that remote already exist # In case of real error the failure will be detected later. pass try: old_commit = g.rev_parse() # Using fetch + checkout ensure caching is effective g.git_cmd( ["fetch", "-f", remote_name, "%s:refs/e3-checkout" % revision]) g.checkout("refs/e3-checkout", force=True) new_commit = g.rev_parse() # Verify that there is no local change p = g.git_cmd(["status", "--porcelain"], output=PIPE) if p.out: logger.error( "Repository %s is locally modified, saving " "diff in stash\n%s", self.name, p.out, ) g.git_cmd(["stash", "save", "-u"]) if old_commit == new_commit: result = ReturnValue.unchanged elif self.compute_changelog: # Fetch the change log and dump it into the changelog file with closing( tempfile.NamedTemporaryFile(mode="w", delete=False)) as fd: g.write_log(fd, rev_range="%s..%s" % (old_commit, new_commit)) tmp_filename = fd.name try: with open(tmp_filename) as fd: commits = [ commit for commit in g.parse_log(fd, max_diff_size=1024) ] finally: rm(tmp_filename) with open(self.changelog_file, "w") as fd: json.dump(commits, fd) # We have removed local changes or updated the git repository result = ReturnValue.success else: result = ReturnValue.success except GitError: logger.exception("Error during git update %s" % self.name) result = ReturnValue.failure return result, old_commit, new_commit
def test_git_repo(): working_tree = os.path.join(os.getcwd(), "working_tree") working_tree2 = os.path.join(os.getcwd(), "working_tree2") repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = os.path.join(working_tree, "new.txt") echo_to_file(new_file, "new\n") commit_note = unixpath(os.path.join(working_tree, "commit_note.txt")) echo_to_file( commit_note, "\n".join(( "Code-Review+2: Nobody <*****@*****.**>", "Submitted-by: Nobody <*****@*****.**>", "Submitted-at: Thu, 08 Jun 2017 18:40:11 +0200", )), ) repo.git_cmd(["add", "new.txt"]) repo.git_cmd(["config", "user.email", "*****@*****.**"]) repo.git_cmd(["config", "user.name", "e3 core"]) repo.git_cmd(["commit", "-m", "new file"]) repo.git_cmd(["tag", "-a", "-m", "new tag", "20.0855369232"]) repo.git_cmd( ["notes", "--ref", "review", "add", "HEAD", "-F", commit_note]) # try with gerrit notes with open("log.txt", "w") as f: repo.write_log(f, with_gerrit_notes=True) with open("log.txt") as f: commits = list(repo.parse_log(f)) assert "*****@*****.**" in commits[0]["notes"]["Code-Review+2"] # try with an invalid note repo.git_cmd([ "notes", "--ref", "review", "add", "HEAD", "-f", "-m", "invalid-note" ]) with open("log.txt", "w") as f: repo.write_log(f, with_gerrit_notes=True) with open("log.txt") as f: commits = list(repo.parse_log(f)) assert commits[0]["notes"] is None # try again without gerrit notes with open("log.txt", "w") as f: repo.write_log(f) with open("log.txt") as f: commits = list(repo.parse_log(f)) assert "new file" in commits[0]["message"] assert commits[0]["email"] == "*****@*****.**" new_sha = commits[0]["sha"] assert "20.0855369232" in repo.describe() assert new_sha == repo.rev_parse() with pytest.raises(GitError) as err: repo.describe("g") assert "describe --always g" in str(err) echo_to_file(new_file, "new line\n", append=True) with open("commit1.diff", "wb") as f: repo.write_local_diff(f) with open("commit1.diff", "rb") as f: assert b"+new line" in f.read() echo_to_file(new_file, 10000 * "*") repo.git_cmd(["commit", "-a", "-m", "file update"]) with open("log2.txt", "w") as f: repo.write_log(f) with open("log2.txt") as f: commits = list(repo.parse_log(f, max_diff_size=1000)) # assert b'diff too long' not in commits[1]['diff'] assert "file update" in commits[0]["message"] assert "diff too long" in commits[0]["diff"] assert "new file" in commits[1]["message"] assert commits[1]["sha"] != commits[0]["sha"] assert commits[1]["diff"] != commits[0]["diff"] repo2 = GitRepository(working_tree2) giturl = "file://%s" % working_tree.replace("\\", "/") repo2.init(url=giturl, remote="tree1") repo2.update(url=giturl, refspec="master") repo2.rev_parse() == repo.rev_parse() repo2.fetch_gerrit_notes(url=giturl) p = repo2.git_cmd(["notes", "--ref=review", "show", new_sha], output=subprocess.PIPE) assert "invalid-note" in p.out
def test_git_repo(): working_tree = os.path.join(os.getcwd(), 'working_tree') working_tree2 = os.path.join(os.getcwd(), 'working_tree2') repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = os.path.join(working_tree, 'new.txt') echo_to_file(new_file, 'new\n') commit_note = unixpath(os.path.join(working_tree, 'commit_note.txt')) echo_to_file( commit_note, '\n'.join( ('Code-Review+2: Nobody <*****@*****.**>', 'Submitted-by: Nobody <*****@*****.**>', 'Submitted-at: Thu, 08 Jun 2017 18:40:11 +0200'))) repo.git_cmd(['add', 'new.txt']) repo.git_cmd(['config', 'user.email', '*****@*****.**']) repo.git_cmd(['config', 'user.name', 'e3 core']) repo.git_cmd(['commit', '-m', 'new file']) repo.git_cmd(['tag', '-a', '-m', 'new tag', '20.0855369232']) repo.git_cmd( ['notes', '--ref', 'review', 'add', 'HEAD', '-F', commit_note]) # try with gerrit notes with open('log.txt', 'w') as f: repo.write_log(f, with_gerrit_notes=True) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert '*****@*****.**' in commits[0]['notes']['Code-Review+2'] # try with an invalid note repo.git_cmd([ 'notes', '--ref', 'review', 'add', 'HEAD', '-f', '-m', 'invalid-note' ]) with open('log.txt', 'w') as f: repo.write_log(f, with_gerrit_notes=True) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert commits[0]['notes'] is None # try again without gerrit notes with open('log.txt', 'w') as f: repo.write_log(f) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert 'new file' in commits[0]['message'] assert commits[0]['email'] == '*****@*****.**' new_sha = commits[0]['sha'] assert '20.0855369232' in repo.describe() assert new_sha == repo.rev_parse() with pytest.raises(GitError) as err: repo.describe('g') assert 'describe --always g' in str(err) echo_to_file(new_file, 'new line\n', append=True) with open('commit1.diff', 'wb') as f: repo.write_local_diff(f) with open('commit1.diff', 'rb') as f: assert b'+new line' in f.read() echo_to_file(new_file, 10000 * '*') repo.git_cmd(['commit', '-a', '-m', 'file update']) with open('log2.txt', 'w') as f: repo.write_log(f) with open('log2.txt', 'r') as f: commits = list(repo.parse_log(f, max_diff_size=1000)) # assert b'diff too long' not in commits[1]['diff'] assert 'file update' in commits[0]['message'] assert 'diff too long' in commits[0]['diff'] assert 'new file' in commits[1]['message'] assert commits[1]['sha'] != commits[0]['sha'] assert commits[1]['diff'] != commits[0]['diff'] repo2 = GitRepository(working_tree2) giturl = 'file://%s' % working_tree.replace('\\', '/') repo2.init(url=giturl, remote='tree1') repo2.update(url=giturl, refspec='master') repo2.rev_parse() == repo.rev_parse() repo2.fetch_gerrit_notes(url=giturl) p = repo2.git_cmd(['notes', '--ref=review', 'show', new_sha], output=subprocess.PIPE) assert 'invalid-note' in p.out