Merge ~racb/git-ubuntu:clone-remote-tracking into git-ubuntu:main

Proposed by Robie Basak
Status: Merged
Merged at revision: e0c2907afa264504df8a25b9470da06a7b38c5cc
Proposed branch: ~racb/git-ubuntu:clone-remote-tracking
Merge into: git-ubuntu:main
Diff against target: 146 lines (+92/-9)
4 files modified
gitubuntu/clone.py (+3/-2)
gitubuntu/git_repository.py (+28/-5)
gitubuntu/git_repository_test.py (+58/-0)
gitubuntu/merge.py (+3/-2)
Reviewer Review Type Date Requested Status
Athos Ribeiro (community) Approve
Server Team CI bot continuous-integration Approve
Canonical Server Reporter Pending
git-ubuntu developers Pending
Review via email: mp+450591@code.launchpad.net

Commit message

Make Jenkins happy

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:e0c2907afa264504df8a25b9470da06a7b38c5cc
https://jenkins.canonical.com/server-team/job/git-ubuntu-ci/39/
Executed test runs:
    SUCCESS: VM Setup
    SUCCESS: Build
    SUCCESS: VM Reset
    SUCCESS: Unit Tests
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.canonical.com/server-team/job/git-ubuntu-ci/39//rebuild

review: Approve (continuous-integration)
Revision history for this message
Athos Ribeiro (athos-ribeiro) wrote :

Thanks, Robie.

A helper or a new function could be extracted from the 2 new test cases (test_create_tracking_branch*) since most of the code in those are duplicated. This shouldn't be a blocker here though since they are short test cases (however, if we ever need to use the same snippets for another test, we should consider extracting that duplicated code snippet somehow).

LGTM

review: Approve
Revision history for this message
Robie Basak (racb) wrote :

Thank you Athos for the review! I agree on the duplication - it's not worth the trade-off for two, but if we need the pattern more, then I would factor the duplication out into a test fixture.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/gitubuntu/clone.py b/gitubuntu/clone.py
index f34b453..4da8d4a 100644
--- a/gitubuntu/clone.py
+++ b/gitubuntu/clone.py
@@ -99,8 +99,9 @@ def main(
9999
100 try:100 try:
101 local_repo.create_tracking_branch(101 local_repo.create_tracking_branch(
102 'ubuntu/devel',102 local_branch_name='ubuntu/devel',
103 'pkg/ubuntu/devel'103 remote_name='pkg',
104 remote_branch_name='ubuntu/devel',
104 )105 )
105 local_repo.checkout_commitish('ubuntu/devel')106 local_repo.checkout_commitish('ubuntu/devel')
106 except:107 except:
diff --git a/gitubuntu/git_repository.py b/gitubuntu/git_repository.py
index 27ed645..e57dc1d 100644
--- a/gitubuntu/git_repository.py
+++ b/gitubuntu/git_repository.py
@@ -2189,15 +2189,38 @@ class GitUbuntuRepository:
2189 _, _, pretty_name = tag.name.partition('refs/tags/')2189 _, _, pretty_name = tag.name.partition('refs/tags/')
2190 return pretty_name2190 return pretty_name
21912191
2192 def create_tracking_branch(self, branch_name, upstream_name, force=False):2192 def create_tracking_branch(
2193 return self.raw_repo.create_branch(2193 self,
2194 branch_name,2194 local_branch_name,
2195 remote_name,
2196 remote_branch_name,
2197 force=False,
2198 ):
2199 """Create a local branch that tracks a remote branch
2200
2201 The remote tracking branch ("refs/remotes/...") must already exist.
2202 Usually this means that "git fetch" must have been run to create it.
2203
2204 :param str local_branch_name: the name of the local branch to create
2205 :param str remote_name: the name of the remote
2206 :param str remote_branch_name: the name of the remote branch
2207 :param bool force: overwrite existing branch
2208 :rtype: pygit2.Branch
2209 :returns: the newly created branch
2210 """
2211 branch = self.raw_repo.create_branch(
2212 local_branch_name,
2195 self.raw_repo.lookup_branch(2213 self.raw_repo.lookup_branch(
2196 upstream_name,2214 f'{remote_name}/{remote_branch_name}',
2197 pygit2.GIT_BRANCH_REMOTE2215 pygit2.GIT_BRANCH_REMOTE,
2198 ).peel(pygit2.Commit),2216 ).peel(pygit2.Commit),
2199 force2217 force
2200 )2218 )
2219 remote_config = f'branch.{local_branch_name}.remote'
2220 branch_config = f'branch.{local_branch_name}.merge'
2221 self.raw_repo.config[remote_config] = remote_name
2222 self.raw_repo.config[branch_config] = f'refs/heads/{local_branch_name}'
2223 return branch
22012224
2202 def checkout_commitish(self, commitish):2225 def checkout_commitish(self, commitish):
2203 # pygit2 checkout does not accept hashes2226 # pygit2 checkout does not accept hashes
diff --git a/gitubuntu/git_repository_test.py b/gitubuntu/git_repository_test.py
index 05bc157..d0bc4fd 100644
--- a/gitubuntu/git_repository_test.py
+++ b/gitubuntu/git_repository_test.py
@@ -1197,3 +1197,61 @@ def test_get_head_info(repo):
1197 commit_id=foo_commit_id,1197 commit_id=foo_commit_id,
1198 ),1198 ),
1199 }1199 }
1200
1201
1202def test_create_tracking_branch(repo, pygit2_repo):
1203 """
1204 :param GitUbuntuRepository repo: fixture providing a temporary
1205 GitUbuntuRepository instance to use
1206 :param pygit2.Repository pygit2_repo: fixture providing a temporary
1207 pygit2.Repository instance to use
1208 """
1209 # Note that we cannot use a repo instance twice, because pytest will give
1210 # us only one instance. A convenient workaround is that the remote repo
1211 # doesn't need to be a GitUbuntuRepository and so the pygit2_repo fixture
1212 # will do as it is independent. We can assert they are definitely
1213 # different:
1214 assert repo.raw_repo is not pygit2_repo
1215 Repo(
1216 commits=[Commit(name='main')],
1217 branches={'main': Placeholder('main')},
1218 ).write(pygit2_repo)
1219 repo.raw_repo.remotes.create('origin', pygit2_repo.path)
1220 repo.git_run(['fetch', 'origin', 'main'])
1221 repo.create_tracking_branch(
1222 local_branch_name='main',
1223 remote_name='origin',
1224 remote_branch_name='main',
1225 )
1226 assert (
1227 repo.raw_repo.references['refs/heads/main'].peel(pygit2.Commit).id
1228 == pygit2_repo.references['refs/heads/main'].peel(pygit2.Commit).id
1229 )
1230
1231
1232def test_create_tracking_branch_tracks(repo, pygit2_repo):
1233 """
1234 :param GitUbuntuRepository repo: fixture providing a temporary
1235 GitUbuntuRepository instance to use
1236 :param pygit2.Repository pygit2_repo: fixture providing a temporary
1237 pygit2.Repository instance to use
1238 """
1239 # Note that we cannot use a repo instance twice, because pytest will give
1240 # us only one instance. A convenient workaround is that the remote repo
1241 # doesn't need to be a GitUbuntuRepository and so the pygit2_repo fixture
1242 # will do as it is independent. We can assert they are definitely
1243 # different:
1244 assert repo.raw_repo is not pygit2_repo
1245 Repo(
1246 commits=[Commit(name='main')],
1247 branches={'main': Placeholder('main')},
1248 ).write(pygit2_repo)
1249 repo.raw_repo.remotes.create('origin', pygit2_repo.path)
1250 repo.git_run(['fetch', 'origin', 'main'])
1251 repo.create_tracking_branch(
1252 local_branch_name='main',
1253 remote_name='origin',
1254 remote_branch_name='main',
1255 )
1256 assert repo.raw_repo.config['branch.main.remote'] == 'origin'
1257 assert repo.raw_repo.config['branch.main.merge'] == 'refs/heads/main'
diff --git a/gitubuntu/merge.py b/gitubuntu/merge.py
index 27678c4..f0ee681 100644
--- a/gitubuntu/merge.py
+++ b/gitubuntu/merge.py
@@ -418,8 +418,9 @@ def main(
418 )418 )
419 try:419 try:
420 branch = repo.create_tracking_branch(420 branch = repo.create_tracking_branch(
421 onto,421 local_branch_name=onto,
422 'pkg/%s' % onto,422 remote_name='pkg',
423 remote_branch_name=onto,
423 force=force,424 force=force,
424 )425 )
425 except:426 except:

Subscribers

People subscribed via source and target branches