Refactor FileDiffActivety (#537)
use androidx @NonNull instead of jetbrains @NotNull Merge branch 'master' into refactor_FileDiffActivity code format corrections Merge branch 'master' into refactor_FileDiffActivity add Unit TESTs for ParseDiff.getFileDiffViewArray add get methods make rm/add similar to gitea stats calc diff stat based on diff itself rename getFileContents -> toString fix empty content bug & add comments format AppUtil.java rm unused working version skip binary files for now ... [DONT CRASH] better regex format FileDiffView.java rename toolbar_title -> toolbarTitle Co-authored-by: 6543 <6543@obermui.de> Reviewed-by: M M Arif <mmarif@noreply.codeberg.org>
This commit is contained in:
parent
5005fcc5b5
commit
f21f23c1f0
@ -10,18 +10,17 @@ import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.adapters.FilesDiffAdapter;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.helpers.AlertDialogs;
|
||||
import org.mian.gitnex.helpers.ParseDiff;
|
||||
import org.mian.gitnex.helpers.Toasty;
|
||||
import org.mian.gitnex.helpers.Version;
|
||||
import org.mian.gitnex.models.FileDiffView;
|
||||
import org.mian.gitnex.util.AppUtil;
|
||||
import org.mian.gitnex.util.TinyDB;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
@ -34,7 +33,7 @@ import retrofit2.Callback;
|
||||
public class FileDiffActivity extends BaseActivity {
|
||||
|
||||
private View.OnClickListener onClickListener;
|
||||
private TextView toolbar_title;
|
||||
private TextView toolbarTitle;
|
||||
private ListView mListView;
|
||||
private ProgressBar mProgressBar;
|
||||
final Context ctx = this;
|
||||
@ -64,13 +63,13 @@ public class FileDiffActivity extends BaseActivity {
|
||||
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
||||
|
||||
ImageView closeActivity = findViewById(R.id.close);
|
||||
toolbar_title = findViewById(R.id.toolbar_title);
|
||||
toolbarTitle = findViewById(R.id.toolbar_title);
|
||||
mListView = findViewById(R.id.listView);
|
||||
mProgressBar = findViewById(R.id.progress_bar);
|
||||
|
||||
mListView.setDivider(null);
|
||||
|
||||
toolbar_title.setText(R.string.processingText);
|
||||
toolbarTitle.setText(R.string.processingText);
|
||||
initCloseListener();
|
||||
closeActivity.setOnClickListener(onClickListener);
|
||||
|
||||
@ -83,7 +82,7 @@ public class FileDiffActivity extends BaseActivity {
|
||||
|
||||
// fallback for old gitea instances
|
||||
if(new Version(tinyDb.getString("giteaVersion")).less("1.13.0")) {
|
||||
apiCall = true;
|
||||
apiCall = false;
|
||||
instanceUrl = tinyDb.getString("instanceUrlWithProtocol");
|
||||
}
|
||||
|
||||
@ -112,69 +111,14 @@ public class FileDiffActivity extends BaseActivity {
|
||||
assert response.body() != null;
|
||||
|
||||
AppUtil appUtil = new AppUtil();
|
||||
List<FileDiffView> fileContentsArray = new ArrayList<>();
|
||||
|
||||
String[] lines = response.body().string().split("diff");
|
||||
|
||||
if(lines.length > 0) {
|
||||
|
||||
for(int i = 1; i < lines.length; i++) {
|
||||
|
||||
if(lines[i].contains("@@ -")) {
|
||||
|
||||
String[] level2nd = lines[i].split("@@ -"); // main content part of single diff view
|
||||
|
||||
String[] fileName_ = level2nd[0].split("\\+\\+\\+ b/"); // filename part
|
||||
String fileNameFinal = fileName_[1];
|
||||
|
||||
String[] fileContents_ = level2nd[1].split("@@"); // file info / content part
|
||||
String fileInfoFinal = fileContents_[0];
|
||||
StringBuilder fileContentsFinal = new StringBuilder(fileContents_[1]);
|
||||
|
||||
if(level2nd.length > 2) {
|
||||
for(int j = 2; j < level2nd.length; j++) {
|
||||
fileContentsFinal.append(level2nd[j]);
|
||||
}
|
||||
}
|
||||
|
||||
String fileExtension = FileUtils.getExtension(fileNameFinal);
|
||||
|
||||
String fileContentsFinalWithBlankLines = fileContentsFinal.toString().replaceAll(".*@@.*", "");
|
||||
String fileContentsFinalWithoutBlankLines = fileContentsFinal.toString().replaceAll(".*@@.*(\r?\n|\r)?", "");
|
||||
fileContentsFinalWithoutBlankLines = fileContentsFinalWithoutBlankLines.replaceAll(".*\\ No newline at end of file.*(\r?\n|\r)?", "");
|
||||
|
||||
fileContentsArray.add(new FileDiffView(fileNameFinal, appUtil.imageExtension(fileExtension), fileInfoFinal, fileContentsFinalWithoutBlankLines));
|
||||
}
|
||||
else {
|
||||
|
||||
String[] getFileName = lines[i].split("--git a/");
|
||||
|
||||
String[] getFileName_ = getFileName[1].split("b/");
|
||||
String getFileNameFinal = getFileName_[0].trim();
|
||||
|
||||
String[] binaryFile = getFileName_[1].split("GIT binary patch");
|
||||
String binaryFileRaw = binaryFile[1].substring(binaryFile[1].indexOf('\n') + 1);
|
||||
String binaryFileFinal = binaryFile[1].substring(binaryFileRaw.indexOf('\n') + 1);
|
||||
|
||||
String fileExtension = FileUtils.getExtension(getFileNameFinal);
|
||||
|
||||
if(appUtil.imageExtension(FileUtils.getExtension(getFileNameFinal))) {
|
||||
|
||||
fileContentsArray.add(new FileDiffView(getFileNameFinal, appUtil.imageExtension(fileExtension), "", binaryFileFinal));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
List<FileDiffView> fileContentsArray = ParseDiff.getFileDiffViewArray(response.body().string());
|
||||
|
||||
int filesCount = fileContentsArray.size();
|
||||
if(filesCount > 1) {
|
||||
toolbar_title.setText(getResources().getString(R.string.fileDiffViewHeader, Integer.toString(filesCount)));
|
||||
toolbarTitle.setText(getResources().getString(R.string.fileDiffViewHeader, Integer.toString(filesCount)));
|
||||
}
|
||||
else {
|
||||
toolbar_title.setText(getResources().getString(R.string.fileDiffViewHeaderSingle, Integer.toString(filesCount)));
|
||||
toolbarTitle.setText(getResources().getString(R.string.fileDiffViewHeaderSingle, Integer.toString(filesCount)));
|
||||
}
|
||||
|
||||
FilesDiffAdapter adapter = new FilesDiffAdapter(ctx, fileContentsArray);
|
||||
|
@ -86,7 +86,7 @@ public class FilesDiffAdapter extends BaseAdapter {
|
||||
FileDiffView data = (FileDiffView) getItem(position);
|
||||
headerFileName.setText(data.getFileName());
|
||||
|
||||
if(data.isFileType()) {
|
||||
if(data.isFileBinary()) {
|
||||
|
||||
diffStats.setVisibility(View.GONE);
|
||||
diffLines.addView(getMessageView(context.getResources().getString(R.string.binaryFileError)));
|
||||
@ -97,7 +97,7 @@ public class FilesDiffAdapter extends BaseAdapter {
|
||||
diffStats.setVisibility(View.VISIBLE);
|
||||
headerFileInfo.setText(data.getFileInfo());
|
||||
|
||||
String[] codeLines = getLines(data.getFileContents());
|
||||
String[] codeLines = getLines(data.toString());
|
||||
|
||||
if(MAXIMUM_LINES > codeLines.length) {
|
||||
|
||||
|
145
app/src/main/java/org/mian/gitnex/helpers/ParseDiff.java
Normal file
145
app/src/main/java/org/mian/gitnex/helpers/ParseDiff.java
Normal file
@ -0,0 +1,145 @@
|
||||
package org.mian.gitnex.helpers;
|
||||
|
||||
import org.mian.gitnex.models.FileDiffView;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Author 6543
|
||||
*/
|
||||
|
||||
public class ParseDiff {
|
||||
|
||||
private static String[] getFileNames(String raw) {
|
||||
|
||||
String[] lines2 = raw.split(" b/");
|
||||
if(lines2.length < 2) {
|
||||
return new String[1];
|
||||
}
|
||||
String oldName = lines2[0];
|
||||
String newName = lines2[1].split("\\n")[0];
|
||||
return new String[]{oldName, newName};
|
||||
}
|
||||
|
||||
private static String getFileInfo(String raw) {
|
||||
|
||||
if(raw.contains("\ndeleted file mode \\d+\n")) {
|
||||
return "delete";
|
||||
}
|
||||
else if(raw.contains("\nnew file mode \\d+\n")) {
|
||||
return "new";
|
||||
}
|
||||
return "change";
|
||||
}
|
||||
|
||||
private static int[] countRemoveAdd(String raw) {
|
||||
|
||||
int rm = 0, add = 0;
|
||||
|
||||
Pattern rmPattern = Pattern.compile("\n-");
|
||||
Pattern addPattern = Pattern.compile("\n\\+");
|
||||
Matcher rmMatcher = rmPattern.matcher(raw);
|
||||
Matcher addMatcher = addPattern.matcher(raw);
|
||||
|
||||
while(rmMatcher.find())
|
||||
rm++;
|
||||
while(addMatcher.find())
|
||||
add++;
|
||||
|
||||
return new int[]{rm, add};
|
||||
}
|
||||
|
||||
public static List<FileDiffView> getFileDiffViewArray(String raw) {
|
||||
|
||||
List<FileDiffView> fileContentsArray = new ArrayList<>();
|
||||
|
||||
String[] lines = raw.split("(^|\\n)diff --git a/");
|
||||
if(lines.length > 1) {
|
||||
|
||||
// for each file in diff
|
||||
for(int i = 1; i < lines.length; i++) {
|
||||
|
||||
// check if it is a binary file
|
||||
if(lines[i].contains("\nBinary files a/")) {
|
||||
String[] fileNames = getFileNames(lines[i]);
|
||||
if(fileNames.length != 2) {
|
||||
continue;
|
||||
}
|
||||
fileContentsArray.add(new FileDiffView(fileNames[0], fileNames[1], "binary", "", null));
|
||||
}
|
||||
|
||||
// check if it is a binary patch
|
||||
else if(lines[i].contains("\nGIT binary patch\n")) {
|
||||
String[] fileNames = getFileNames(lines[i]);
|
||||
if(fileNames.length != 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] tmp = lines[i].split("literal \\d+\\n");
|
||||
String rawContent = "";
|
||||
if(tmp.length >= 2) {
|
||||
rawContent = tmp[1].replace("\n", "");
|
||||
}
|
||||
|
||||
List<FileDiffView.Content> contents = new ArrayList<>();
|
||||
contents.add(new FileDiffView.Content(rawContent));
|
||||
fileContentsArray.add(new FileDiffView(fileNames[0], fileNames[1], "binary", getFileInfo(lines[i]), contents));
|
||||
}
|
||||
|
||||
// check if it is normal diff
|
||||
else if(lines[i].contains("\n@@ -")) {
|
||||
String[] fileNames = getFileNames(lines[i]);
|
||||
if(fileNames.length != 2) {
|
||||
continue;
|
||||
}
|
||||
String[] rawDiffs = lines[i].split("\n@@ -");
|
||||
if(rawDiffs.length <= 1) {
|
||||
continue;
|
||||
}
|
||||
List<FileDiffView.Content> contents = new ArrayList<>();
|
||||
// parse each section starting with "@@" at line beginning
|
||||
for(int j = 1; j < rawDiffs.length; j++) {
|
||||
// remove stats info (ending with @@)
|
||||
// raw diff is the whole raw diff without any diff meta info's
|
||||
String[] rawDiff = rawDiffs[j].split("^\\d+(,\\d+)? \\+\\d+(,\\d+)? @@");
|
||||
if(rawDiff.length <= 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// extract the diff stats info of the first line
|
||||
String statsLine = rawDiffs[j].split("\n")[0].split(" @@")[0];
|
||||
|
||||
// parse "-1,2 +2,3" and "-1 -3" and so on
|
||||
int oldStart = 0, newStart = 0;
|
||||
String diffPos[] = statsLine.split(" \\+");
|
||||
if(diffPos.length == 2) {
|
||||
oldStart = Integer.parseInt(diffPos[0].split(",")[0]);
|
||||
newStart = Integer.parseInt(diffPos[1].split(",")[0]);
|
||||
|
||||
}
|
||||
|
||||
// get stat
|
||||
int[] stats = countRemoveAdd(rawDiff[1]);
|
||||
|
||||
contents.add(new FileDiffView.Content(rawDiff[1], oldStart, newStart, stats[0], stats[1]));
|
||||
}
|
||||
fileContentsArray.add(new FileDiffView(fileNames[0], fileNames[1], "diff", getFileInfo(lines[i]), contents));
|
||||
}
|
||||
|
||||
// a rename
|
||||
else if(lines[i].contains("\nrename from")) {
|
||||
String[] lines2 = lines[i].split("\\nrename (from|to )");
|
||||
if(lines2.length != 3) {
|
||||
continue;
|
||||
}
|
||||
fileContentsArray.add(new FileDiffView(lines2[1], lines2[2].split("\\n")[0], "rename", "rename", null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fileContentsArray;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package org.mian.gitnex.helpers;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import androidx.annotation.NonNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
@ -70,7 +70,7 @@ public class Version {
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
public boolean equal(@NotNull Version v) {
|
||||
public boolean equal(@NonNull Version v) {
|
||||
|
||||
int rounds = Math.min(this.values.size(), v.values.size());
|
||||
for(int i = 0; i < rounds; i++) {
|
||||
@ -101,7 +101,7 @@ public class Version {
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
public boolean less(@NotNull Version v) {
|
||||
public boolean less(@NonNull Version v) {
|
||||
|
||||
int rounds = Math.min(this.values.size(), v.values.size());
|
||||
for(int i = 0; i < rounds; i++) {
|
||||
@ -142,7 +142,7 @@ public class Version {
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
public boolean higher(@NotNull Version v) {
|
||||
public boolean higher(@NonNull Version v) {
|
||||
|
||||
int rounds = Math.min(this.values.size(), v.values.size());
|
||||
for(int i = 0; i < rounds; i++) {
|
||||
@ -182,7 +182,7 @@ public class Version {
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
public boolean lessOrEqual(@NotNull Version v) {
|
||||
public boolean lessOrEqual(@NonNull Version v) {
|
||||
|
||||
int rounds = Math.min(this.values.size(), v.values.size());
|
||||
for(int i = 0; i < rounds; i++) {
|
||||
@ -213,7 +213,7 @@ public class Version {
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
public boolean higherOrEqual(@NotNull Version v) {
|
||||
public boolean higherOrEqual(@NonNull Version v) {
|
||||
|
||||
int rounds = Math.min(this.values.size(), v.values.size());
|
||||
for(int i = 0; i < rounds; i++) {
|
||||
@ -225,4 +225,4 @@ public class Version {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,159 @@
|
||||
package org.mian.gitnex.models;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Author M M Arif
|
||||
* Author 6543
|
||||
*/
|
||||
|
||||
public class FileDiffView {
|
||||
|
||||
private String fileName;
|
||||
private boolean fileType;
|
||||
private String fileInfo;
|
||||
private String fileContents;
|
||||
private String fileNewName;
|
||||
private String fileOldName;
|
||||
private String diffType;
|
||||
private String fileInfo;
|
||||
private Stats stats;
|
||||
private List<Content> contents;
|
||||
|
||||
public FileDiffView(String fileName, boolean fileType, String fileInfo, String fileContents)
|
||||
{
|
||||
public class Stats {
|
||||
|
||||
this.fileName = fileName;
|
||||
this.fileType = fileType;
|
||||
this.fileInfo = fileInfo;
|
||||
this.fileContents = fileContents;
|
||||
private int lineAdded;
|
||||
private int lineRemoved;
|
||||
|
||||
}
|
||||
public Stats(int added, int removed) {
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
this.lineAdded = added;
|
||||
this.lineRemoved = removed;
|
||||
}
|
||||
|
||||
public boolean isFileType() {
|
||||
return fileType;
|
||||
}
|
||||
public int getAdded() {
|
||||
|
||||
public String getFileInfo() {
|
||||
return fileInfo;
|
||||
}
|
||||
return lineAdded;
|
||||
}
|
||||
|
||||
public int getRemoved() {
|
||||
|
||||
return lineRemoved;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String toString() {
|
||||
|
||||
return "+" + this.lineAdded + ", -" + this.lineRemoved;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Content {
|
||||
|
||||
private int lineAdded;
|
||||
private int lineRemoved;
|
||||
private int oldLineStart;
|
||||
private int newLineStart;
|
||||
private String raw;
|
||||
|
||||
public Content(String content) {
|
||||
|
||||
this.raw = content;
|
||||
}
|
||||
|
||||
public Content(String content, int oldStart, int newStart, int removed, int added) {
|
||||
|
||||
this.raw = content;
|
||||
this.lineAdded = added;
|
||||
this.lineRemoved = removed;
|
||||
this.oldLineStart = oldStart;
|
||||
this.newLineStart = newStart;
|
||||
}
|
||||
|
||||
public String getRaw() {
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
public int getLineAdded() {
|
||||
|
||||
return this.lineAdded;
|
||||
}
|
||||
|
||||
public int getLineRemoved() {
|
||||
|
||||
return this.lineRemoved;
|
||||
}
|
||||
|
||||
public int getOldLineStart() {
|
||||
|
||||
return this.oldLineStart;
|
||||
}
|
||||
|
||||
public int getNewLineStart() {
|
||||
|
||||
return this.newLineStart;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public FileDiffView(String oldName, String newName, String diffType, String fileInfo, List<Content> fileContents) {
|
||||
|
||||
this.fileNewName = newName.trim();
|
||||
this.fileOldName = oldName.trim();
|
||||
this.diffType = diffType;
|
||||
this.fileInfo = fileInfo;
|
||||
this.contents = fileContents;
|
||||
this.stats = new Stats(0, 0);
|
||||
if(fileContents != null) {
|
||||
for(Content content : this.contents) {
|
||||
stats.lineAdded += content.lineAdded;
|
||||
stats.lineRemoved += content.lineRemoved;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
|
||||
if(fileOldName.length() != 0 && !fileOldName.equals(fileNewName)) {
|
||||
return fileOldName + " -> " + fileNewName;
|
||||
}
|
||||
return fileNewName;
|
||||
}
|
||||
|
||||
public boolean isFileBinary() {
|
||||
|
||||
return diffType.equals("binary");
|
||||
}
|
||||
|
||||
public String getFileInfo() {
|
||||
|
||||
if(diffType.equals("binary")) {
|
||||
return diffType + " " + fileInfo;
|
||||
}
|
||||
|
||||
if(fileInfo.equals("change") && this.stats != null) {
|
||||
return this.stats.toString();
|
||||
}
|
||||
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String toString() {
|
||||
|
||||
StringBuilder raw = new StringBuilder();
|
||||
if(this.contents != null) {
|
||||
for(Content c : this.contents) {
|
||||
raw.append(c.getRaw());
|
||||
}
|
||||
}
|
||||
return raw.toString();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public List<Content> getFileContents() {
|
||||
|
||||
return this.contents;
|
||||
}
|
||||
|
||||
public String getFileContents() {
|
||||
return fileContents;
|
||||
}
|
||||
}
|
||||
|
@ -23,271 +23,278 @@ import java.util.Locale;
|
||||
|
||||
public class AppUtil {
|
||||
|
||||
public static String strReplace(String str, String original, String replace) {
|
||||
return str.replace(original, replace);
|
||||
}
|
||||
|
||||
public static boolean haveNetworkConnection(Context context) {
|
||||
boolean haveConnectedWifi = false;
|
||||
boolean haveConnectedMobile = false;
|
||||
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
assert cm != null;
|
||||
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
|
||||
for (NetworkInfo ni : netInfo) {
|
||||
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
|
||||
if (ni.isConnected())
|
||||
haveConnectedWifi = true;
|
||||
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
|
||||
if (ni.isConnected())
|
||||
haveConnectedMobile = true;
|
||||
}
|
||||
return haveConnectedWifi || haveConnectedMobile;
|
||||
}
|
||||
|
||||
public static int getAppBuildNo(Context context) {
|
||||
try {
|
||||
PackageInfo packageInfo = context.getPackageManager()
|
||||
.getPackageInfo(context.getPackageName(), 0);
|
||||
return packageInfo.versionCode;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new RuntimeException("Could not get package name: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAppVersion(Context context) {
|
||||
try {
|
||||
PackageInfo packageInfo = context.getPackageManager()
|
||||
.getPackageInfo(context.getPackageName(), 0);
|
||||
return packageInfo.versionName;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new RuntimeException("Could not get package name: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public int charactersLength(String str) {
|
||||
return str.length();
|
||||
}
|
||||
|
||||
public Boolean checkStringsWithAlphaNumeric(String str) { // [a-zA-Z0-9]
|
||||
return str.matches("^[\\w]+$");
|
||||
}
|
||||
|
||||
public Boolean checkStrings(String str) { // [a-zA-Z0-9-_. ]
|
||||
return str.matches("^[\\w .-]+$");
|
||||
}
|
||||
|
||||
public Boolean checkStringsWithAlphaNumericDashDotUnderscore(String str) { // [a-zA-Z0-9-_]
|
||||
return str.matches("^[\\w.-]+$");
|
||||
}
|
||||
|
||||
public Boolean checkStringsWithDash(String str) { // [a-zA-Z0-9-_. ]
|
||||
return str.matches("^[\\w-]+$");
|
||||
}
|
||||
|
||||
public Boolean checkIntegers(String str) {
|
||||
return str.matches("\\d+");
|
||||
}
|
||||
|
||||
public int getResponseStatusCode(String u) throws Exception {
|
||||
|
||||
URL url = new URL(u);
|
||||
HttpURLConnection http = (HttpURLConnection) url.openConnection();
|
||||
return (http.getResponseCode());
|
||||
|
||||
}
|
||||
|
||||
public static void setAppLocale(Resources resource, String locCode) {
|
||||
|
||||
DisplayMetrics dm = resource.getDisplayMetrics();
|
||||
Configuration config = resource.getConfiguration();
|
||||
config.setLocale(new Locale(locCode.toLowerCase()));
|
||||
resource.updateConfiguration(config, dm);
|
||||
|
||||
}
|
||||
|
||||
public static boolean httpCheck(String url) {
|
||||
|
||||
String pattern = "^(http|https)://.*$";
|
||||
return url.matches(pattern);
|
||||
|
||||
}
|
||||
|
||||
public static String formatFileSize(long size) {
|
||||
|
||||
String repoSize = null;
|
||||
|
||||
double m = size/1024.0;
|
||||
double g = ((size/1024.0)/1024.0);
|
||||
double t = (((size/1024.0)/1024.0)/1024.0);
|
||||
|
||||
DecimalFormat dec = new DecimalFormat("0.00");
|
||||
|
||||
if ( t > 1 ) {
|
||||
repoSize = dec.format(t).concat(" TB");
|
||||
}
|
||||
else if ( g > 1 ) {
|
||||
repoSize = dec.format(g).concat(" GB");
|
||||
}
|
||||
else if ( m > 1 ) {
|
||||
repoSize = dec.format(m).concat(" MB");
|
||||
}
|
||||
else if ( (double) size > 1 ) {
|
||||
repoSize = dec.format((double) size).concat(" KB");
|
||||
}
|
||||
|
||||
return repoSize;
|
||||
|
||||
}
|
||||
|
||||
public static String formatFileSizeInDetail(long size) {
|
||||
public static String strReplace(String str, String original, String replace) {
|
||||
|
||||
String fileSize = null;
|
||||
return str.replace(original, replace);
|
||||
}
|
||||
|
||||
double k = size/1024.0;
|
||||
double m = ((size/1024.0)/1024.0);
|
||||
double g = (((size/1024.0)/1024.0)/1024.0);
|
||||
double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0);
|
||||
public static boolean haveNetworkConnection(Context context) {
|
||||
|
||||
DecimalFormat dec = new DecimalFormat("0.00");
|
||||
boolean haveConnectedWifi = false;
|
||||
boolean haveConnectedMobile = false;
|
||||
|
||||
if ( t > 1 ) {
|
||||
fileSize = dec.format(t).concat(" TB");
|
||||
}
|
||||
else if ( g > 1 ) {
|
||||
fileSize = dec.format(g).concat(" GB");
|
||||
}
|
||||
else if ( m > 1 ) {
|
||||
fileSize = dec.format(m).concat(" MB");
|
||||
}
|
||||
else if ( k > 1 ) {
|
||||
fileSize = dec.format(k).concat(" KB");
|
||||
}
|
||||
else if ( (double) size > 1 ) {
|
||||
fileSize = dec.format((double) size).concat(" B");
|
||||
}
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
assert cm != null;
|
||||
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
|
||||
for(NetworkInfo ni : netInfo) {
|
||||
if(ni.getTypeName().equalsIgnoreCase("WIFI")) {
|
||||
if(ni.isConnected()) {
|
||||
haveConnectedWifi = true;
|
||||
}
|
||||
}
|
||||
if(ni.getTypeName().equalsIgnoreCase("MOBILE")) {
|
||||
if(ni.isConnected()) {
|
||||
haveConnectedMobile = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return haveConnectedWifi || haveConnectedMobile;
|
||||
}
|
||||
|
||||
return fileSize;
|
||||
public static int getAppBuildNo(Context context) {
|
||||
|
||||
}
|
||||
try {
|
||||
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
||||
return packageInfo.versionCode;
|
||||
}
|
||||
catch(PackageManager.NameNotFoundException e) {
|
||||
throw new RuntimeException("Could not get package name: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String customDateFormat(String customDate) {
|
||||
public static String getAppVersion(Context context) {
|
||||
|
||||
String[] parts = customDate.split("-");
|
||||
final String year = parts[0];
|
||||
final String month = parts[1];
|
||||
final String day = parts[2];
|
||||
try {
|
||||
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
||||
return packageInfo.versionName;
|
||||
}
|
||||
catch(PackageManager.NameNotFoundException e) {
|
||||
throw new RuntimeException("Could not get package name: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
String sMonth;
|
||||
if (Integer.parseInt(month) < 10) {
|
||||
sMonth = "0"+ month;
|
||||
} else {
|
||||
sMonth = month;
|
||||
}
|
||||
public int charactersLength(String str) {
|
||||
|
||||
return str.length();
|
||||
}
|
||||
|
||||
String sDay;
|
||||
if (Integer.parseInt(day) < 10) {
|
||||
sDay = "0"+ day;
|
||||
} else {
|
||||
sDay = day;
|
||||
}
|
||||
public Boolean checkStringsWithAlphaNumeric(String str) { // [a-zA-Z0-9]
|
||||
return str.matches("^[\\w]+$");
|
||||
}
|
||||
|
||||
return year + "-" + sMonth + "-" + sDay;
|
||||
public Boolean checkStrings(String str) { // [a-zA-Z0-9-_. ]
|
||||
return str.matches("^[\\w .-]+$");
|
||||
}
|
||||
|
||||
}
|
||||
public Boolean checkStringsWithAlphaNumericDashDotUnderscore(String str) { // [a-zA-Z0-9-_]
|
||||
return str.matches("^[\\w.-]+$");
|
||||
}
|
||||
|
||||
public static String customDateCombine(String customDate) {
|
||||
public Boolean checkStringsWithDash(String str) { // [a-zA-Z0-9-_. ]
|
||||
return str.matches("^[\\w-]+$");
|
||||
}
|
||||
|
||||
final Calendar c = Calendar.getInstance();
|
||||
int mHour = c.get(Calendar.HOUR_OF_DAY);
|
||||
int mMinute = c.get(Calendar.MINUTE);
|
||||
int mSeconds = c.get(Calendar.SECOND);
|
||||
public Boolean checkIntegers(String str) {
|
||||
|
||||
String sMin;
|
||||
if ((mMinute) < 10) {
|
||||
sMin = "0"+ mMinute;
|
||||
} else {
|
||||
sMin = String.valueOf(mMinute);
|
||||
}
|
||||
return str.matches("\\d+");
|
||||
}
|
||||
|
||||
public int getResponseStatusCode(String u) throws Exception {
|
||||
|
||||
URL url = new URL(u);
|
||||
HttpURLConnection http = (HttpURLConnection) url.openConnection();
|
||||
return (http.getResponseCode());
|
||||
|
||||
}
|
||||
|
||||
public static void setAppLocale(Resources resource, String locCode) {
|
||||
|
||||
String sSec;
|
||||
if ((mSeconds) < 10) {
|
||||
sSec = "0"+ mSeconds;
|
||||
} else {
|
||||
sSec = String.valueOf(mSeconds);
|
||||
}
|
||||
DisplayMetrics dm = resource.getDisplayMetrics();
|
||||
Configuration config = resource.getConfiguration();
|
||||
config.setLocale(new Locale(locCode.toLowerCase()));
|
||||
resource.updateConfiguration(config, dm);
|
||||
|
||||
return (customDate + "T" + mHour + ":" + sMin + ":" + sSec + "Z");
|
||||
}
|
||||
|
||||
}
|
||||
public static boolean httpCheck(String url) {
|
||||
|
||||
public String encodeBase64(String str) {
|
||||
String pattern = "^(http|https)://.*$";
|
||||
return url.matches(pattern);
|
||||
|
||||
}
|
||||
|
||||
public static String formatFileSize(long size) {
|
||||
|
||||
String repoSize = null;
|
||||
|
||||
double m = size / 1024.0;
|
||||
double g = ((size / 1024.0) / 1024.0);
|
||||
double t = (((size / 1024.0) / 1024.0) / 1024.0);
|
||||
|
||||
DecimalFormat dec = new DecimalFormat("0.00");
|
||||
|
||||
if(t > 1) {
|
||||
repoSize = dec.format(t).concat(" TB");
|
||||
}
|
||||
else if(g > 1) {
|
||||
repoSize = dec.format(g).concat(" GB");
|
||||
}
|
||||
else if(m > 1) {
|
||||
repoSize = dec.format(m).concat(" MB");
|
||||
}
|
||||
else if((double) size > 1) {
|
||||
repoSize = dec.format((double) size).concat(" KB");
|
||||
}
|
||||
|
||||
String base64Str = str;
|
||||
if(!str.equals("")) {
|
||||
byte[] data = str.getBytes(StandardCharsets.UTF_8);
|
||||
base64Str = Base64.encodeToString(data, Base64.DEFAULT);
|
||||
}
|
||||
return repoSize;
|
||||
|
||||
return base64Str;
|
||||
}
|
||||
|
||||
}
|
||||
public static String formatFileSizeInDetail(long size) {
|
||||
|
||||
public String decodeBase64(String str) {
|
||||
String fileSize = null;
|
||||
|
||||
String base64Str = str;
|
||||
if(!str.equals("")) {
|
||||
byte[] data = Base64.decode(base64Str, Base64.DEFAULT);
|
||||
base64Str = new String(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
double k = size / 1024.0;
|
||||
double m = ((size / 1024.0) / 1024.0);
|
||||
double g = (((size / 1024.0) / 1024.0) / 1024.0);
|
||||
double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0);
|
||||
|
||||
return base64Str;
|
||||
DecimalFormat dec = new DecimalFormat("0.00");
|
||||
|
||||
}
|
||||
if(t > 1) {
|
||||
fileSize = dec.format(t).concat(" TB");
|
||||
}
|
||||
else if(g > 1) {
|
||||
fileSize = dec.format(g).concat(" GB");
|
||||
}
|
||||
else if(m > 1) {
|
||||
fileSize = dec.format(m).concat(" MB");
|
||||
}
|
||||
else if(k > 1) {
|
||||
fileSize = dec.format(k).concat(" KB");
|
||||
}
|
||||
else if((double) size > 1) {
|
||||
fileSize = dec.format((double) size).concat(" B");
|
||||
}
|
||||
|
||||
public Boolean sourceCodeExtension(String ext) {
|
||||
return fileSize;
|
||||
|
||||
String[] extValues = new String[] {"md", "json", "java", "go", "php", "c", "cc", "cpp", "h", "cxx", "cyc", "m",
|
||||
"cs", "bash", "sh", "bsh", "cv", "python", "perl", "pm", "rb", "ruby", "javascript",
|
||||
"coffee", "rc", "rs", "rust", "basic", "clj", "css", "dart", "lisp", "erl", "hs", "lsp", "rkt",
|
||||
"ss", "llvm", "ll", "lua", "matlab", "pascal", "r", "scala", "sql", "latex", "tex", "vb", "vbs",
|
||||
"vhd", "tcl", "wiki.meta", "yaml", "yml", "markdown", "xml", "proto", "regex", "py", "pl", "js",
|
||||
"html", "htm", "volt", "ini", "htaccess", "conf", "gitignore", "gradle", "txt", "properties", "bat",
|
||||
"twig"};
|
||||
}
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
public static String customDateFormat(String customDate) {
|
||||
|
||||
}
|
||||
String[] parts = customDate.split("-");
|
||||
final String year = parts[0];
|
||||
final String month = parts[1];
|
||||
final String day = parts[2];
|
||||
|
||||
public Boolean pdfExtension(String ext) {
|
||||
String sMonth;
|
||||
if(Integer.parseInt(month) < 10) {
|
||||
sMonth = "0" + month;
|
||||
}
|
||||
else {
|
||||
sMonth = month;
|
||||
}
|
||||
|
||||
String[] extValues = new String[] {"pdf"};
|
||||
String sDay;
|
||||
if(Integer.parseInt(day) < 10) {
|
||||
sDay = "0" + day;
|
||||
}
|
||||
else {
|
||||
sDay = day;
|
||||
}
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
return year + "-" + sMonth + "-" + sDay;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean imageExtension(String ext) {
|
||||
public static String customDateCombine(String customDate) {
|
||||
|
||||
String[] extValues = new String[] {"jpg", "jpeg", "gif", "png", "ico"};
|
||||
final Calendar c = Calendar.getInstance();
|
||||
int mHour = c.get(Calendar.HOUR_OF_DAY);
|
||||
int mMinute = c.get(Calendar.MINUTE);
|
||||
int mSeconds = c.get(Calendar.SECOND);
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
String sMin;
|
||||
if((mMinute) < 10) {
|
||||
sMin = "0" + mMinute;
|
||||
}
|
||||
else {
|
||||
sMin = String.valueOf(mMinute);
|
||||
}
|
||||
|
||||
}
|
||||
String sSec;
|
||||
if((mSeconds) < 10) {
|
||||
sSec = "0" + mSeconds;
|
||||
}
|
||||
else {
|
||||
sSec = String.valueOf(mSeconds);
|
||||
}
|
||||
|
||||
public Boolean excludeFilesInFileViewerExtension(String ext) {
|
||||
return (customDate + "T" + mHour + ":" + sMin + ":" + sSec + "Z");
|
||||
|
||||
String[] extValues = new String[] {"doc", "docx", "ppt", "pptx", "xls", "xlsx", "xlsm", "odt",
|
||||
"ott", "odf", "ods", "ots", "exe", "jar", "odg", "otg", "odp", "otp", "bin", "dmg", "psd",
|
||||
"xcf"};
|
||||
}
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
public String encodeBase64(String str) {
|
||||
|
||||
}
|
||||
String base64Str = str;
|
||||
if(!str.equals("")) {
|
||||
byte[] data = str.getBytes(StandardCharsets.UTF_8);
|
||||
base64Str = Base64.encodeToString(data, Base64.DEFAULT);
|
||||
}
|
||||
|
||||
public String getLastCharactersOfWord( String str, int count ) {
|
||||
return base64Str;
|
||||
|
||||
return str.substring(str.length() - count);
|
||||
}
|
||||
|
||||
public String decodeBase64(String str) {
|
||||
|
||||
String base64Str = str;
|
||||
if(!str.equals("")) {
|
||||
byte[] data = Base64.decode(base64Str, Base64.DEFAULT);
|
||||
base64Str = new String(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
return base64Str;
|
||||
|
||||
}
|
||||
|
||||
public Boolean sourceCodeExtension(String ext) {
|
||||
|
||||
String[] extValues = new String[]{"md", "json", "java", "go", "php", "c", "cc", "cpp", "h", "cxx", "cyc", "m", "cs", "bash", "sh", "bsh", "cv", "python", "perl", "pm", "rb", "ruby", "javascript", "coffee", "rc", "rs", "rust", "basic", "clj", "css", "dart", "lisp", "erl", "hs", "lsp", "rkt", "ss", "llvm", "ll", "lua", "matlab", "pascal", "r", "scala", "sql", "latex", "tex", "vb", "vbs", "vhd", "tcl", "wiki.meta", "yaml", "yml", "markdown", "xml", "proto", "regex", "py", "pl", "js", "html", "htm", "volt", "ini", "htaccess", "conf", "gitignore", "gradle", "txt", "properties", "bat", "twig"};
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
|
||||
}
|
||||
|
||||
public Boolean pdfExtension(String ext) {
|
||||
|
||||
String[] extValues = new String[]{"pdf"};
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
|
||||
}
|
||||
|
||||
public Boolean imageExtension(String ext) {
|
||||
|
||||
String[] extValues = new String[]{"jpg", "jpeg", "gif", "png", "ico"};
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
|
||||
}
|
||||
|
||||
public Boolean excludeFilesInFileViewerExtension(String ext) {
|
||||
|
||||
String[] extValues = new String[]{"doc", "docx", "ppt", "pptx", "xls", "xlsx", "xlsm", "odt", "ott", "odf", "ods", "ots", "exe", "jar", "odg", "otg", "odp", "otp", "bin", "dmg", "psd", "xcf"};
|
||||
|
||||
return Arrays.asList(extValues).contains(ext);
|
||||
|
||||
}
|
||||
|
||||
public String getLastCharactersOfWord(String str, int count) {
|
||||
|
||||
return str.substring(str.length() - count);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
51
app/src/test/java/org/mian/gitnex/helpers/ParseDiffTest.java
Normal file
51
app/src/test/java/org/mian/gitnex/helpers/ParseDiffTest.java
Normal file
@ -0,0 +1,51 @@
|
||||
package org.mian.gitnex.helpers;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mian.gitnex.models.FileDiffView;
|
||||
import java.util.List;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Author 6543
|
||||
*/
|
||||
|
||||
public class ParseDiffTest {
|
||||
|
||||
private String commitDiff = "diff --git a/blob.bin b/blob.bin\n" + "deleted file mode 100644\n" + "index 0d73bd2..0000000\n" + "Binary files a/blob.bin and /dev/null differ\n" + "diff --git a/newOne.txt b/newOne.txt\n" + "new file mode 100644\n" + "index 0000000..d46eed0\n" + "--- /dev/null\n" + "+++ b/newOne.txt\n" + "@@ -0,0 +1,2 @@\n" + "+a new file\n" + "+is ok\n" + "diff --git a/toDel.txt b/toDel.txt\n" + "deleted file mode 100644\n" + "index db7b61d..0000000\n" + "--- a/toDel.txt\n" + "+++ /dev/null\n" + "@@ -1,6 +0,0 @@\n" + "-fdsafew\n" + "-fcdsafd\n" + "-saf\n" + "-dsa\n" + "-fds\n" + "-af\n" + "diff --git a/wow.txt b/wow.txt\n" + "index 92e7b0a..c6f2550 100644\n" + "--- a/wow.txt\n" + "+++ b/wow.txt\n" + "@@ -1 +1 @@\n" + "-no newLN\n" + "\\ No newline at end of file\n" + "+no newLN";
|
||||
private String pullDiff = "diff --git a/blob.bin b/blob.bin\n" + "deleted file mode 100644\n" + "index 0d73bd22356cdedbda15802490dbba5677c0bf82..0000000000000000000000000000000000000000\n" + "GIT binary patch\n" + "literal 0\n" + "HcmV?d00001\n" + "\n" + "literal 1048576\n" + "zcmV(nK=QwPuB28ikRJ&1O;k}2;ZJdPRlcSh&H3ZzL{FUXh$7I70$=OgJ&|C6l9n5^\n" + "z)($UbdKG~u1=kru;-zP`c59V|o(6s6w`nh4BZjq62uO^~OO=?T8h^2Wd<-s#e_iI~\n" + "zURkze=$MhaJ$k?4&39P$(BT6=fYUYoKa5hsMqqt;d=e0W61jUQXj5h&>9Mf9aHlLn\n" + "zL?z4mKo3>xs9>gwR}Cx#ZeNWKy0M;5CRQHq%6NVh5=X<1a8P)FsE7yASIj-Jq{{ex\n" + "z2A~tfI3XyuL5>6ot@$SpZItcakZOhIGXKm-LOOpY_XVQ@3=@2EfJq~TXJfiw$1e+3\n" + "zGr~C0-C$O1g|{ndCbn0qMk{!T3Yz523kz;7D*LkRfhH7eYxM4fpC`qUAu4irPjtJy\n" + "Jx`FK##b8Ms9rXYJ\n" + "diff --git a/newOne.txt b/newOne.txt\n" + "new file mode 100644\n" + "index 0000000..d46eed0\n" + "--- /dev/null\n" + "+++ b/newOne.txt\n" + "@@ -0,0 +1,2 @@\n" + "+a new file\n" + "+is ok\n" + "diff --git a/toDel.txt b/toDel.txt\n" + "deleted file mode 100644\n" + "index db7b61d..0000000\n" + "--- a/toDel.txt\n" + "+++ /dev/null\n" + "@@ -1,6 +0,0 @@\n" + "-fdsafew\n" + "-fcdsafd\n" + "-saf\n" + "-dsa\n" + "-fds\n" + "-af\n" + "diff --git a/wow.txt b/wow.txt\n" + "index 92e7b0a..c6f2550 100644\n" + "--- a/wow.txt\n" + "+++ b/wow.txt\n" + "@@ -1 +1 @@\n" + "-no newLN\n" + "\\ No newline at end of file\n" + "+no newLN";
|
||||
|
||||
@Test
|
||||
public void parseCommitDiff() {
|
||||
|
||||
List<FileDiffView> parsed = ParseDiff.getFileDiffViewArray(commitDiff);
|
||||
assertEquals(4, parsed.size());
|
||||
assertTrue(parsed.get(0).isFileBinary());
|
||||
assertFalse(parsed.get(1).isFileBinary());
|
||||
|
||||
assertEquals(1, parsed.get(1).getFileContents().size());
|
||||
assertEquals(2, parsed.get(1).getFileContents().get(0).getLineAdded());
|
||||
assertEquals(0, parsed.get(1).getFileContents().get(0).getLineRemoved());
|
||||
|
||||
assertEquals(1, parsed.get(3).getFileContents().get(0).getLineAdded());
|
||||
assertEquals(1, parsed.get(3).getFileContents().get(0).getLineRemoved());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsePullDiff() {
|
||||
|
||||
List<FileDiffView> parsed = ParseDiff.getFileDiffViewArray(pullDiff);
|
||||
assertEquals(4, parsed.size());
|
||||
assertTrue(parsed.get(0).isFileBinary());
|
||||
assertFalse(parsed.get(1).isFileBinary());
|
||||
|
||||
assertEquals(1, parsed.get(1).getFileContents().size());
|
||||
assertEquals(2, parsed.get(1).getFileContents().get(0).getLineAdded());
|
||||
assertEquals(0, parsed.get(1).getFileContents().get(0).getLineRemoved());
|
||||
|
||||
assertEquals(1, parsed.get(3).getFileContents().get(0).getLineAdded());
|
||||
assertEquals(1, parsed.get(3).getFileContents().get(0).getLineRemoved());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user