feat: deactivate sendignore

This commit is contained in:
Tien Do Nam
2025-02-19 04:05:58 +01:00
parent c9de11711f
commit 19b98d850b
3 changed files with 49 additions and 67 deletions
@@ -9,7 +9,7 @@ import 'package:localsend_app/util/native/cache_helper.dart';
import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/content_uri_helper.dart';
import 'package:localsend_app/util/native/cross_file_converters.dart';
import 'package:localsend_app/util/native/send_ignore.dart';
import 'package:localsend_app/util/send_ignore.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:refena_flutter/refena_flutter.dart';
@@ -153,12 +153,20 @@ class AddDirectoryAction extends AsyncReduxAction<SelectedSendingFilesNotifier,
_logger.info('Reading files in $directoryPath');
final newFiles = <CrossFile>[];
final directoryName = p.basename(directoryPath);
final ignoreManager = IgnoreManager(directoryPath);
final globs = await ignoreManager.getGlobs();
final sendIgnore = SendIgnore();
await for (final entity in Directory(directoryPath).list(recursive: true)) {
if (entity is File) {
final relative = '$directoryName/${p.relative(entity.path, from: directoryPath).replaceAll('\\', '/')}';
if (await checkIgnoreValidity(relative, globs)) {
final innerRelative = p.relative(entity.path, from: directoryPath).replaceAll('\\', '/');
final relative = '$directoryName/$innerRelative';
if (sendIgnore.isIgnoreFile(p.basename(entity.path))) {
sendIgnore.loadIgnoreContent(
parentPath: innerRelative.contains('/') ? p.dirname(innerRelative) : null,
ignoreContents: await entity.readAsLines(),
);
_logger.info('Loaded ignore file: $innerRelative');
continue;
} else if (sendIgnore.isIgnored(innerRelative)) {
_logger.info('Ignored: $innerRelative');
continue;
}
-62
View File
@@ -1,62 +0,0 @@
import 'dart:io';
import 'package:glob/glob.dart';
import 'package:path/path.dart' as p;
const String ignoreName = '.sendignore';
const String globalIgnoreDirectory = '';
Future<bool> checkIgnoreValidity(String relativePath, Set<Glob> globs) async {
return globs.any((glob) => glob.matches(relativePath));
}
class IgnoreManager {
final String directoryPath;
const IgnoreManager(this.directoryPath);
Future<Set<Glob>> getGlobs() async {
final availablePaths = ignorePathCandidates.skipWhile((ignorePath) => !File('$ignorePath/$ignoreName').existsSync()).toList();
if (availablePaths.isEmpty) {
return {};
}
final firstCandidate = availablePaths.first;
// final String projectIgnorePath = '$directoryPath/$ignoreName';
// if (!await File(projectIgnorePath).exists()) {
// final String globalIgnorePath = '$globalIgnoreDirectory/$ignoreName';
// if (!await File(globalIgnorePath).exists()) {
// return {};
// }
// return SendIgnore(globalIgnorePath).globs;
// }
return SendIgnore(firstCandidate).globs;
}
List<String> get ignorePathCandidates {
return [directoryPath, globalIgnoreDirectory];
}
}
class SendIgnore {
final String directoryPath;
const SendIgnore(this.directoryPath);
Future<Set<Glob>> get globs async {
File ignoreFile = File('$directoryPath/$ignoreName');
return makeGlobs(ignoreFile);
}
Future<Set<Glob>> makeGlobs(File ignoreFile) async {
// if (!await ignoreFile.exists()) {
// return {};
// }
final List<String> ignoreContents = await ignoreFile.readAsLines();
final Set<Glob> ignoreGlobs = ignoreContents.map((line) => Glob('$directoryBaseName/$line')).toSet();
return ignoreGlobs;
}
String get directoryBaseName {
return p.basename(directoryPath);
}
}
+36
View File
@@ -0,0 +1,36 @@
import 'package:glob/glob.dart';
// const _ignoreFileName = '.sendignore';
class SendIgnore {
final List<Glob> _globs = [];
SendIgnore();
bool isIgnoreFile(String fileName) {
// TODO: Glob does not provide the same gitignore experience
return false;
// return fileName == _ignoreFileName;
}
void loadIgnoreContent({
required String? parentPath,
required List<String> ignoreContents,
}) {
_globs.addAll(ignoreContents.map((line) {
if (line.startsWith('/')) {
return Glob('$parentPath$line');
} else {
if (parentPath == null) {
return Glob(line);
} else {
return Glob('$parentPath/**/$line');
}
}
}));
}
bool isIgnored(String relativePath) {
return _globs.any((glob) => glob.matches('/$relativePath'));
}
}