Skip to content

Commit

Permalink
Merge pull request orgzly-revived#191 from amberin/check-orgzlyignore…
Browse files Browse the repository at this point in the history
…-file-during-storeBook

Git: Check .orgzlyignore when writing files
  • Loading branch information
amberin authored Mar 14, 2024
2 parents b14d3da + 8f41889 commit c49597b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/src/main/java/com/orgzly/android/repos/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.util.Log;

import com.orgzly.BuildConfig;
import com.orgzly.R;
import com.orgzly.android.App;
import com.orgzly.android.BookName;
import com.orgzly.android.db.entity.Repo;
import com.orgzly.android.git.GitFileSynchronizer;
Expand Down Expand Up @@ -42,6 +44,7 @@
public class GitRepo implements SyncRepo, TwoWaySyncRepo {
private final static String TAG = GitRepo.class.getName();
private final long repoId;
private final Context context = App.getAppContext();

/**
* Used as cause when we try to clone into a non-empty directory
Expand Down Expand Up @@ -186,6 +189,8 @@ public boolean isAutoSyncSupported() {

public VersionedRook storeBook(File file, String fileName) throws IOException {
File destination = synchronizer.repoDirectoryFile(fileName);
ensureRepoPathIsNotIgnored(destination.getPath());

if (destination.exists()) {
synchronizer.updateAndCommitExistingFile(file, fileName);
} else {
Expand Down Expand Up @@ -231,7 +236,7 @@ private VersionedRook currentVersionedRook(Uri uri) {

private IgnoreNode getIgnores() throws IOException {
IgnoreNode ignores = new IgnoreNode();
File ignoreFile = synchronizer.repoDirectoryFile(".orgzlyignore");
File ignoreFile = synchronizer.repoDirectoryFile(context.getString(R.string.repo_ignore_rules_file));
if (ignoreFile.exists()) {
FileInputStream in = new FileInputStream(ignoreFile);
try {
Expand All @@ -243,6 +248,14 @@ private IgnoreNode getIgnores() throws IOException {
return ignores;
}

private void ensureRepoPathIsNotIgnored(String filePath) throws IOException {
IgnoreNode ignores = getIgnores();
if (ignores.isIgnored(filePath, false) == IgnoreNode.MatchResult.IGNORED) {
throw new IOException(context.getString(R.string.error_file_matches_repo_ignore_rule,
context.getString(R.string.repo_ignore_rules_file)));
}
}

public boolean isUnchanged() throws IOException {
// Check if the current head is unchanged.
// If so, we can read all the VersionedRooks from the database.
Expand Down Expand Up @@ -310,6 +323,7 @@ public void delete(Uri uri) throws IOException {
public VersionedRook renameBook(Uri oldUri, String newRookName) throws IOException {
String oldFileName = oldUri.toString().replaceFirst("^/", "");
String newFileName = newRookName + ".org";
ensureRepoPathIsNotIgnored(newFileName);
if (synchronizer.renameFileInRepo(oldFileName, newFileName)) {
synchronizer.tryPush();
return currentVersionedRook(Uri.EMPTY.buildUpon().appendPath(newFileName).build());
Expand Down
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 @@ -766,6 +766,7 @@
<string name="error_generate_ssh_key">Error while trying to generate the SSH key pair</string>
<string name="ssh_key_error_dialog_text">Message: \n</string>
<string name="ssh_key_locked_and_no_activity">Error: SSH key can only be unlocked from an activity</string>
<string name="error_file_matches_repo_ignore_rule">Repository filename matches a rule in %s</string>

<!-- Sync status -->
<string name="sync_status_no_change">No change</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings_untranslatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<string name="git_default_branch" translatable="false">master</string>
<string name="certificates_hint">-----BEGIN CERTIFICATE-----</string>
<string name="webdav" translatable="false">WebDAV</string>
<string name="repo_ignore_rules_file" translatable="false">.orgzlyignore</string>

<string name="lorem_ipsum" translatable="false">Lorem ipsum dolor sit amet, consectetur</string>
<string name="lorem_ipsum_rich" translatable="false">Lorem *ipsum* dolor sit amet, /consectetur/</string>
Expand Down

0 comments on commit c49597b

Please sign in to comment.