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
1diff --git a/gitubuntu/clone.py b/gitubuntu/clone.py
2index f34b453..4da8d4a 100644
3--- a/gitubuntu/clone.py
4+++ b/gitubuntu/clone.py
5@@ -99,8 +99,9 @@ def main(
6
7 try:
8 local_repo.create_tracking_branch(
9- 'ubuntu/devel',
10- 'pkg/ubuntu/devel'
11+ local_branch_name='ubuntu/devel',
12+ remote_name='pkg',
13+ remote_branch_name='ubuntu/devel',
14 )
15 local_repo.checkout_commitish('ubuntu/devel')
16 except:
17diff --git a/gitubuntu/git_repository.py b/gitubuntu/git_repository.py
18index 27ed645..e57dc1d 100644
19--- a/gitubuntu/git_repository.py
20+++ b/gitubuntu/git_repository.py
21@@ -2189,15 +2189,38 @@ class GitUbuntuRepository:
22 _, _, pretty_name = tag.name.partition('refs/tags/')
23 return pretty_name
24
25- def create_tracking_branch(self, branch_name, upstream_name, force=False):
26- return self.raw_repo.create_branch(
27- branch_name,
28+ def create_tracking_branch(
29+ self,
30+ local_branch_name,
31+ remote_name,
32+ remote_branch_name,
33+ force=False,
34+ ):
35+ """Create a local branch that tracks a remote branch
36+
37+ The remote tracking branch ("refs/remotes/...") must already exist.
38+ Usually this means that "git fetch" must have been run to create it.
39+
40+ :param str local_branch_name: the name of the local branch to create
41+ :param str remote_name: the name of the remote
42+ :param str remote_branch_name: the name of the remote branch
43+ :param bool force: overwrite existing branch
44+ :rtype: pygit2.Branch
45+ :returns: the newly created branch
46+ """
47+ branch = self.raw_repo.create_branch(
48+ local_branch_name,
49 self.raw_repo.lookup_branch(
50- upstream_name,
51- pygit2.GIT_BRANCH_REMOTE
52+ f'{remote_name}/{remote_branch_name}',
53+ pygit2.GIT_BRANCH_REMOTE,
54 ).peel(pygit2.Commit),
55 force
56 )
57+ remote_config = f'branch.{local_branch_name}.remote'
58+ branch_config = f'branch.{local_branch_name}.merge'
59+ self.raw_repo.config[remote_config] = remote_name
60+ self.raw_repo.config[branch_config] = f'refs/heads/{local_branch_name}'
61+ return branch
62
63 def checkout_commitish(self, commitish):
64 # pygit2 checkout does not accept hashes
65diff --git a/gitubuntu/git_repository_test.py b/gitubuntu/git_repository_test.py
66index 05bc157..d0bc4fd 100644
67--- a/gitubuntu/git_repository_test.py
68+++ b/gitubuntu/git_repository_test.py
69@@ -1197,3 +1197,61 @@ def test_get_head_info(repo):
70 commit_id=foo_commit_id,
71 ),
72 }
73+
74+
75+def test_create_tracking_branch(repo, pygit2_repo):
76+ """
77+ :param GitUbuntuRepository repo: fixture providing a temporary
78+ GitUbuntuRepository instance to use
79+ :param pygit2.Repository pygit2_repo: fixture providing a temporary
80+ pygit2.Repository instance to use
81+ """
82+ # Note that we cannot use a repo instance twice, because pytest will give
83+ # us only one instance. A convenient workaround is that the remote repo
84+ # doesn't need to be a GitUbuntuRepository and so the pygit2_repo fixture
85+ # will do as it is independent. We can assert they are definitely
86+ # different:
87+ assert repo.raw_repo is not pygit2_repo
88+ Repo(
89+ commits=[Commit(name='main')],
90+ branches={'main': Placeholder('main')},
91+ ).write(pygit2_repo)
92+ repo.raw_repo.remotes.create('origin', pygit2_repo.path)
93+ repo.git_run(['fetch', 'origin', 'main'])
94+ repo.create_tracking_branch(
95+ local_branch_name='main',
96+ remote_name='origin',
97+ remote_branch_name='main',
98+ )
99+ assert (
100+ repo.raw_repo.references['refs/heads/main'].peel(pygit2.Commit).id
101+ == pygit2_repo.references['refs/heads/main'].peel(pygit2.Commit).id
102+ )
103+
104+
105+def test_create_tracking_branch_tracks(repo, pygit2_repo):
106+ """
107+ :param GitUbuntuRepository repo: fixture providing a temporary
108+ GitUbuntuRepository instance to use
109+ :param pygit2.Repository pygit2_repo: fixture providing a temporary
110+ pygit2.Repository instance to use
111+ """
112+ # Note that we cannot use a repo instance twice, because pytest will give
113+ # us only one instance. A convenient workaround is that the remote repo
114+ # doesn't need to be a GitUbuntuRepository and so the pygit2_repo fixture
115+ # will do as it is independent. We can assert they are definitely
116+ # different:
117+ assert repo.raw_repo is not pygit2_repo
118+ Repo(
119+ commits=[Commit(name='main')],
120+ branches={'main': Placeholder('main')},
121+ ).write(pygit2_repo)
122+ repo.raw_repo.remotes.create('origin', pygit2_repo.path)
123+ repo.git_run(['fetch', 'origin', 'main'])
124+ repo.create_tracking_branch(
125+ local_branch_name='main',
126+ remote_name='origin',
127+ remote_branch_name='main',
128+ )
129+ assert repo.raw_repo.config['branch.main.remote'] == 'origin'
130+ assert repo.raw_repo.config['branch.main.merge'] == 'refs/heads/main'
131diff --git a/gitubuntu/merge.py b/gitubuntu/merge.py
132index 27678c4..f0ee681 100644
133--- a/gitubuntu/merge.py
134+++ b/gitubuntu/merge.py
135@@ -418,8 +418,9 @@ def main(
136 )
137 try:
138 branch = repo.create_tracking_branch(
139- onto,
140- 'pkg/%s' % onto,
141+ local_branch_name=onto,
142+ remote_name='pkg',
143+ remote_branch_name=onto,
144 force=force,
145 )
146 except:

Subscribers

People subscribed via source and target branches