Skip to content

Commit

Permalink
Allow following file renames in commit history activity
Browse files Browse the repository at this point in the history
Fixes #1419
  • Loading branch information
maniac103 committed Feb 10, 2025
1 parent 5e512b9 commit 9598b44
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
40 changes: 39 additions & 1 deletion app/src/main/java/com/gh4a/activities/CommitHistoryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

Expand All @@ -27,6 +31,7 @@ public static Intent makeIntent(Context context, String repoOwner, String repoNa
private String mRef;
private String mFilePath;
private boolean mSupportBaseSelection;
private boolean mFollowRenames;

@Nullable
@Override
Expand All @@ -52,14 +57,47 @@ protected void onInitExtras(Bundle extras) {

@Override
protected Fragment onCreateFragment() {
return CommitListFragment.newInstance(mRepoOwner, mRepoName, mRef, mFilePath);
CommitListFragment f = CommitListFragment.newInstance(mRepoOwner, mRepoName, mRef, mFilePath);
f.setFollowFileRenames(mFollowRenames);
return f;
}

@Override
protected Intent navigateUp() {
return RepositoryActivity.makeIntent(this, mRepoOwner, mRepoName, mRef);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.commit_history_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem followItem = menu.findItem(R.id.follow_renames);
if (followItem != null) {
followItem.setChecked(mFollowRenames);
}
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.follow_renames) {
mFollowRenames = !mFollowRenames;
item.setChecked(mFollowRenames);
CommitListFragment f = (CommitListFragment) getFragment();
if (f != null) {
f.setFollowFileRenames(mFollowRenames);
}
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public boolean baseSelectionAllowed() {
return mSupportBaseSelection;
Expand Down
72 changes: 71 additions & 1 deletion app/src/main/java/com/gh4a/fragment/CommitListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
import com.meisolsson.githubsdk.service.repositories.RepositoryCommitService;

import java.net.HttpURLConnection;
import java.util.List;
import java.util.Optional;

import io.reactivex.Maybe;
import io.reactivex.Single;
import retrofit2.Response;

Expand All @@ -57,6 +60,8 @@ public interface ContextSelectionCallback {
private String mRepoName;
private String mRef;
private String mFilePath;
private boolean mFollowRenames;
private RenameFollowData mRenameFollowData;

private CommitAdapter mAdapter;
private ContextSelectionCallback mCallback;
Expand Down Expand Up @@ -85,6 +90,13 @@ public static CommitListFragment newInstance(String repoOwner, String repoName,
return f;
}

public void setFollowFileRenames(boolean followRenames) {
if (mFollowRenames != followRenames) {
mFollowRenames = followRenames;
onRefresh();
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -155,7 +167,29 @@ public boolean onContextItemSelected(MenuItem item) {
protected Single<Response<Page<Commit>>> loadPage(int page, boolean bypassCache) {
final RepositoryCommitService service =
ServiceFactory.get(RepositoryCommitService.class, bypassCache);
return service.getCommits(mRepoOwner, mRepoName, mRef, mFilePath, page)
final String ref = mRenameFollowData != null ? mRenameFollowData.ref : mRef;
final String filePath = mRenameFollowData != null ? mRenameFollowData.fileName : mFilePath;

return service.getCommits(mRepoOwner, mRepoName, ref, filePath, page)
.flatMap(response -> {
Page<Commit> commits = response.isSuccessful() ? response.body() : null;
if (commits != null && commits.next() == null && mFollowRenames) {
final List<Commit> items = commits.items();
final Commit last = items.get(items.size() - 1);
return determineFollowData(last, filePath)
.map(data -> {
mRenameFollowData = data;
return Response.success(Page.<Commit>builder()
.items(items)
.next(1)
.prev(commits.prev())
.build());
})
.defaultIfEmpty(response)
.toSingle();
}
return Single.just(response);
})
.map(response -> {
// 409 is returned for empty repos
if (response.code() == HttpURLConnection.HTTP_CONFLICT) {
Expand All @@ -164,4 +198,40 @@ protected Single<Response<Page<Commit>>> loadPage(int page, boolean bypassCache)
return response;
});
}

@Override
protected void resetSubject() {
super.resetSubject();
mRenameFollowData = null;
}

private Maybe<RenameFollowData> determineFollowData(Commit commit, String currentFileName) {
final RepositoryCommitService service =
ServiceFactory.get(RepositoryCommitService.class, false);
return service.getCommit(mRepoOwner, mRepoName, commit.sha())
.filter(Response::isSuccessful)
.map(Response::body)
.map(actualCommit -> {
if (actualCommit != null && actualCommit.files() != null) {
return actualCommit.files().stream()
.filter(f -> f.filename().equals(currentFileName) &&
f.previousFilename() != null)
.findFirst()
.map(f -> {
RenameFollowData data = new RenameFollowData();
data.fileName = f.previousFilename();
data.ref = actualCommit.parents().get(0).sha();
return data;
});
}
return Optional.<RenameFollowData>empty();
})
.filter(Optional::isPresent)
.map(Optional::get);
}

private static class RenameFollowData {
String ref;
String fileName;
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/menu/commit_history_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/follow_renames"
android:checkable="true"
android:title="@string/follow_renames" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<string name="no_browser_found">The link could not be opened: no browser was found on your device</string>
<string name="link_copied">Link copied</string>
<string name="wrap_lines">Wrap lines</string>
<string name="follow_renames">Follow renames</string>
<string name="unknown">unknown</string>
<string name="history">History</string>
<string name="download">Download</string>
Expand Down

0 comments on commit 9598b44

Please sign in to comment.