Skip to content

Commit 7c9d494

Browse files
committed
Add instructions for using with subtrees.
Subtrees are the primary way I expect people to use the `java-repo-tools` repository. Since it's not a super common workflow, I document the needed steps in the README.
1 parent 237c693 commit 7c9d494

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

README.md

+163
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,169 @@ in the [GoogleCloudPlaftorm](https://github.com/GoogleCloudPlatform)
88
organization.
99

1010

11+
## Using this repository
12+
13+
This repository is copied into a subtree of other Java repositories, such as
14+
[java-docs-samples](/GoogleCloudPlatform/java-docs-samples). Note, that a
15+
subtree is just the code copied into a directory, so a regular `git clone` will
16+
continue to work.
17+
18+
19+
### Adding to a new repository
20+
21+
To copy `java-repo-tools` into a subtree of a new repository `my-java-samples`,
22+
first add this repository as a remote. We then fetch all the changes from this
23+
`java-repo-tools`.
24+
25+
```
26+
git remote add java-repo-tools [email protected]:GoogleCloudPlatform/java-repo-tools.git
27+
git fetch java-repo-tools master
28+
```
29+
30+
To make it easier to push changes back upstream, create a new branch.
31+
32+
```
33+
git checkout -b java-repo-tools java-repo-tools/master
34+
```
35+
36+
We can then go back to the `my-java-samples` code and prepare a Pull Request to
37+
add the `java-repo-tools` code in a subtree.
38+
39+
```
40+
git checkout master
41+
# Making a new branch ia optional, but recommended to send a pull request to
42+
# start using java-repo-tools.
43+
git checkout -b use-java-repo-tools
44+
git read-tree --prefix=java-repo-tools/ -u java-repo-tools
45+
```
46+
47+
Now all the content of `java-repo-tools` will be in the `java-repo-tools/`
48+
directory (which we specified in the `--prefix` command).
49+
50+
#### Using the Maven configuration
51+
52+
If all the projects within your `my-java-samples` share a common parent POM for
53+
plugin configuration (like checkstyle). We can then make the
54+
`java-repo-tools/pom.xml` parent of this.
55+
56+
```
57+
<!-- Parent POM defines common plugins and properties. -->
58+
<parent>
59+
<groupId>com.google.cloud</groupId>
60+
<artifactId>shared-configuration</artifactId>
61+
<version>1.0.0</version>
62+
<relativePath>java-repo-tools</relativePath>
63+
</parent>
64+
```
65+
66+
Once this is added to the common parent, all modules will have the same plugin
67+
configuration applied. If the children POMs provide the plugin information
68+
themselves, it will override this configuration, so you should delete any
69+
now-redundant plugin information.
70+
71+
72+
#### Examples
73+
74+
- Adding to repository with an existing parent POM: Pull Request
75+
[java-docs-samples#125][java-docs-samples-125].
76+
77+
[java-docs-samples-125]: https://github.com/GoogleCloudPlatform/java-docs-samples/pull/125
78+
79+
80+
### Detecting if you need to synchronize a subtree
81+
82+
If you haven't done this before, run
83+
84+
```
85+
git remote add java-repo-tools
86+
[email protected]:GoogleCloudPlatform/java-repo-tools.git
87+
git fetch java-repo-tools master
88+
# Optional, but it makes pushing changes upstream easier.
89+
git checkout -b java-repo-tools java-repo-tools/master
90+
```
91+
92+
To detect if you have changes in the directory, run
93+
94+
```
95+
git fetch java-repo-tools
96+
git diff-tree -p HEAD:java-repo-tools/ java-repo-tools/master
97+
```
98+
99+
or to diff against your local `java-repo-tools` branch:
100+
101+
```
102+
git diff-tree -p HEAD:java-repo-tools/ java-repo-tools --
103+
```
104+
105+
(The trailing `--` is to say that we want to compare against the branch, not the
106+
directory.)
107+
108+
109+
### Pulling changes from Java Repository Tools to a subtree
110+
111+
To update the `java-repo-tools` directory, if you haven't done this before, run
112+
113+
```
114+
git remote add java-repo-tools
115+
[email protected]:GoogleCloudPlatform/java-repo-tools.git
116+
git fetch java-repo-tools master
117+
git checkout -b java-repo-tools java-repo-tools/master
118+
```
119+
120+
To pull the changes when in this branch run
121+
122+
```
123+
git pull java-repo-tools master
124+
```
125+
126+
To pull the changes back from upstream:
127+
128+
```
129+
git checkout java-repo-tools
130+
git pull java-repo-tools master
131+
```
132+
133+
Pull them into the main code.
134+
135+
```
136+
git checkout master
137+
# Making a new branch is optional, but recommended to send a pull request for
138+
# update.
139+
git checkout -b update-java-repo-tools
140+
git merge --squash -Xsubtree=java-repo-tools/ --no-commit java-repo-tools
141+
```
142+
143+
Then you can make any needed changes to make the rest of the repository
144+
compatible with the updated `java-repo-tools` code, commit, push, and send a
145+
Pull Request as you would in the normal flow.
146+
147+
148+
### Pushing changes from a subtree upstream to Java Repository Tools
149+
150+
What if you make changes in your repository and now want to push them upstream?
151+
152+
Assuming you just commited changes in the `java-repo-tools/` directory of your
153+
`my-main-branch`, to merge the changes into the local `java-repo-tools` branch,
154+
we need to cherry pick this commit using the subtree strategy. It will ignore
155+
any changes to file not in the `java-repo-tools/` directory.
156+
157+
```
158+
git checkout java-repo-tools
159+
git cherry-pick -x --strategy=subtree my-main-branch
160+
```
161+
162+
After you have committed all the changes you want to your `java-repo-tools`
163+
branch, you can push to the upstream `java-repo-tools` repository with the
164+
following command. (Replace `name-for-remote-branch` with the name you'd like to
165+
give the branch on the `java-repo-tools` repository.)
166+
167+
```
168+
git push java-repo-tools java-repo-tools:name-for-remote-branch
169+
```
170+
171+
Then, you can send a pull request to the `java-repo-tools` repository.
172+
173+
11174
## Contributing changes
12175

13176
- See [CONTRIBUTING.md](CONTRIBUTING.md)

0 commit comments

Comments
 (0)