|
29 | 29 | import java.io.IOException;
|
30 | 30 |
|
31 | 31 | import java.util.ArrayList;
|
32 |
| -import java.util.Collection; |
33 | 32 |
|
34 | 33 | import androidx.annotation.NonNull;
|
35 | 34 | import androidx.annotation.Nullable;
|
|
41 | 40 | import org.eclipse.jgit.api.Git;
|
42 | 41 | import org.eclipse.jgit.api.errors.GitAPIException;
|
43 | 42 | import org.eclipse.jgit.api.errors.JGitInternalException;
|
44 |
| -import org.eclipse.jgit.lib.AnyObjectId; |
| 43 | +import org.eclipse.jgit.lib.ProgressMonitor; |
45 | 44 | import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
46 | 45 |
|
47 | 46 | import io.syslogic.github.R;
|
|
65 | 64 | *
|
66 | 65 | * @author Martin Zeitler
|
67 | 66 | */
|
68 |
| -public class RepositoryFragment extends BaseFragment implements TokenCallback, CloneCommand.Callback { |
| 67 | +public class RepositoryFragment extends BaseFragment implements TokenCallback, ProgressMonitor { |
69 | 68 |
|
70 | 69 | /** Log Tag */
|
71 | 70 | @SuppressWarnings("unused")
|
@@ -180,47 +179,44 @@ public void onNothingSelected(AdapterView<?> parent) {}
|
180 | 179 | }
|
181 | 180 | }
|
182 | 181 | if (destination.exists()) {
|
183 |
| - gitClone(destination); |
| 182 | + String branch = getDataBinding().toolbarDownload.spinnerBranch.getSelectedItem().toString(); |
| 183 | + gitClone(destination, branch); |
184 | 184 | }
|
185 | 185 | });
|
186 | 186 | }
|
187 | 187 | }
|
188 | 188 | return this.mDataBinding.getRoot();
|
189 | 189 | }
|
190 | 190 |
|
191 |
| - /** TODO: git clone (experimental). */ |
192 |
| - private void gitClone(@NonNull File destination) { |
| 191 | + /** git clone. */ |
| 192 | + private void gitClone(@NonNull File destination, @Nullable String branch) { |
193 | 193 | Thread thread = new Thread(() -> {
|
194 | 194 | CloneCommand cmd = Git.cloneRepository()
|
195 | 195 | .setURI(getRepoUrl())
|
196 |
| - .setDirectory(destination) |
197 |
| - .setRemote("github") |
198 |
| - .setCloneAllBranches(true) |
199 | 196 | .setCredentialsProvider(new UsernamePasswordCredentialsProvider(getPersonalAccessToken(), ""))
|
200 |
| - .setCallback(RepositoryFragment.this); |
201 |
| - /* |
202 |
| - .setTransportConfigCallback(transport -> { |
203 |
| - Map<String, String> headers = new HashMap<>(); |
204 |
| - headers.put("Authorization", getPersonalAccessToken()); |
205 |
| - TransportHttp http = (TransportHttp) transport; |
206 |
| - http.setAdditionalHeaders(headers); |
207 |
| - }); |
208 |
| - */ |
209 |
| - try { |
210 |
| - if (mDebug) {Log.d(LOG_TAG, "cloning into " + getRepoName());} |
211 |
| - Git result = cmd.call(); |
212 |
| - org.eclipse.jgit.lib.Repository repo = result.getRepository(); |
213 |
| - repo.close(); |
| 197 | + .setProgressMonitor(RepositoryFragment.this) |
| 198 | + .setDirectory(destination) |
| 199 | + .setRemote("github"); |
214 | 200 |
|
| 201 | + if (branch == null) { |
| 202 | + cmd.setCloneAllBranches(true); |
| 203 | + } else { |
| 204 | + cmd.setBranch(branch); |
| 205 | + } |
| 206 | + |
| 207 | + try { |
| 208 | + if (mDebug) {Log.d(LOG_TAG, "Cloning into " + getRepoName() + "...");} |
| 209 | + cmd.call(); |
215 | 210 | } catch (GitAPIException | JGitInternalException | NoSuchMethodError e) {
|
216 | 211 | String message = e.getMessage();
|
217 | 212 | if (mDebug) {Log.e(LOG_TAG, e.getMessage(), e);}
|
218 | 213 | requireActivity().runOnUiThread(() ->
|
219 | 214 | Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show());
|
220 | 215 | } finally {
|
221 |
| - if (mDebug) { |
222 |
| - Log.d(LOG_TAG, "cloning complete"); |
223 |
| - } |
| 216 | + String message = "Cloned."; |
| 217 | + if (mDebug) {Log.d(LOG_TAG, message);} |
| 218 | + requireActivity().runOnUiThread(() -> |
| 219 | + Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show()); |
224 | 220 | }
|
225 | 221 | });
|
226 | 222 | thread.start();
|
@@ -555,21 +551,34 @@ private void switchToolbarView(@NonNull Integer childIndex) {
|
555 | 551 | @Override
|
556 | 552 | public void onLogin(@NonNull User item) {}
|
557 | 553 |
|
558 |
| - /** Interface: CloneCommand.Callback */ |
| 554 | + /** Interface: ProgressMonitor */ |
| 555 | + @Override |
| 556 | + public void start(int totalTasks) { |
| 557 | + if (mDebug) {Log.d(LOG_TAG, "totalTasks: " + totalTasks);} |
| 558 | + } |
| 559 | + |
| 560 | + /** Interface: ProgressMonitor */ |
| 561 | + @Override |
| 562 | + public void beginTask(String title, int totalWork) { |
| 563 | + if (mDebug) {Log.d(LOG_TAG, "beginTask " + title + ": " + totalWork);} |
| 564 | + } |
| 565 | + |
| 566 | + /** Interface: ProgressMonitor */ |
559 | 567 | @Override
|
560 |
| - public void initializedSubmodules(Collection<String> submodules) { |
561 |
| - if (mDebug) {Log.d(LOG_TAG, "initializedSubmodules");} |
| 568 | + public void update(int completed) { |
| 569 | + if (mDebug) {Log.d(LOG_TAG, "completed: +" + completed);} |
562 | 570 | }
|
563 | 571 |
|
564 |
| - /** Interface: CloneCommand.Callback */ |
| 572 | + /** Interface: ProgressMonitor */ |
565 | 573 | @Override
|
566 |
| - public void cloningSubmodule(String path) { |
567 |
| - if (mDebug) {Log.d(LOG_TAG, "cloningSubmodule");} |
| 574 | + public void endTask() { |
| 575 | + if (mDebug) {Log.d(LOG_TAG, "endTask");} |
568 | 576 | }
|
569 | 577 |
|
570 |
| - /** Interface: CloneCommand.Callback */ |
| 578 | + /** Interface: ProgressMonitor */ |
571 | 579 | @Override
|
572 |
| - public void checkingOut(AnyObjectId commit, String path) { |
573 |
| - if (mDebug) {Log.d(LOG_TAG, "checkingOut");} |
| 580 | + public boolean isCancelled() { |
| 581 | + // if (mDebug) {Log.d(LOG_TAG, "isCancelled?");} |
| 582 | + return false; |
574 | 583 | }
|
575 | 584 | }
|
0 commit comments