def check_branch(repo, reference, old_ref, new_ref): reference = lib.map_to_upstream_branch(reference) # Allow updates to tags. Unchecked for now. if reference.startswith(TAGS): return if not reference.startswith(HEADS): raise ReferenceNotInHeads() # Get changes as seen on the local and remote branches respectively. try: client_changes = change.Changes.from_range(repo, reference, new_ref) except git.exc.GitCommandError: # The branch doesn't exist yet. Just return, allowing the update. return for client_change in client_changes: try: server_change = change.Change( repo.commit( rev=get_change_branch(reference, client_change.id))) except git.exc.BadName: # Don't have this change in the context of this branch yet. No worries. server_change = change.Change() if server_change not in client_change: raise change.Conflict(server_change.id) if client_change in server_change: # The server is up to date on this change. Go to the next. continue # TODO(Carl) Each change is updated as soon as we verify that it is up # to date. Since there may be many changes, we could have some updated # while later ones don't get updated. I make no attempt to roll back # the ones which have been updated. # 1. For gerrit style code review, each change is independent. It isn't # so bad that earlier changes may get updated while laters ones # don't. I think it is acceptable. # 2. For github style code review, the branch won't get updated. # Updates to the earlier changes will be unreachable from any branch # until you fix up the entire branch and successfully update all of # the changes in the branch. I should give some thought to updating # all of the changes atomically. git.refs.symbolic.SymbolicReference.create( repo=repo, path=get_change_branch(reference, client_change.id), reference=client_change.head, force=True)
def test_id(self): commit1 = mock.Mock(predecessors=[]) commit2 = mock.Mock(predecessors=[commit1]) c = change.Change(commit2) self.assertEqual(str(commit1), c.id) self.assertEqual(str(commit2), c.head)
def test___iter__(self): commit1 = mock.Mock(predecessors=[]) commit2 = mock.Mock(predecessors=[commit1]) commit3 = mock.Mock(predecessors=[commit2]) c1 = change.Change(commit3) self.assertEqual([commit1, commit2, commit3], list(c1))
def test___contains__(self): commit1 = mock.Mock(predecessors=[]) commit2 = mock.Mock(predecessors=[commit1]) c1 = change.Change(commit1) c2 = change.Change(commit2) self.assertIn(c1, c2) self.assertNotIn(c2, c1) commit3 = mock.Mock(predecessors=[commit1]) c3 = change.Change(commit3) self.assertIn(c1, c3) self.assertNotIn(c3, c1) self.assertNotIn(c3, c2) self.assertNotIn(c2, c3)
def test___sub__(self): commit1 = mock.Mock(predecessors=[]) commit2 = mock.Mock(predecessors=[commit1]) commit3 = mock.Mock(predecessors=[commit2]) commit4 = mock.Mock(predecessors=[commit2]) commit5 = mock.Mock(predecessors=[commit4]) c1 = change.Change(commit3) c2 = change.Change(commit5) self.assertEqual([commit3], c1 - c2) self.assertEqual([commit4, commit5], c2 - c1) c3 = change.Change(commit2) self.assertEqual([], c3 - c2) self.assertEqual([], c3 - c1) self.assertEqual([commit3], c1 - c3) self.assertEqual([commit4, commit5], c2 - c3)
def command_id(repo, program_name, args): if len(args) == 0: args = ["HEAD"] for arg in args: c = change.Change(repo.commit(rev=arg)) print(c.id)