Skip to content

Commit

Permalink
fix problems related to javadoc and indent
Browse files Browse the repository at this point in the history
  • Loading branch information
nawalchahboune committed Feb 11, 2025
1 parent 687d4fd commit 7477e94
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 34 deletions.
1 change: 1 addition & 0 deletions jabref
Submodule jabref added at 470597
112 changes: 84 additions & 28 deletions src/main/java/org/jabref/gui/autosaveandbackup/BackupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ public static void rewriteFile(Path dbFile, String content) throws IOException {
LOGGER.info("Successfully rewrote the file at path: {}", dbFile);
}

// Helper method to normalize BibTeX content
/**
* Normalizes the BibTeX content by trimming spaces, removing blank lines, and reassembling with line breaks.
* This is needed to ensure consistent formatting of BibTeX entries, which helps in comparing and processing them.
*
* @param input The raw BibTeX content.
* @return The normalized BibTeX content.
*/
private static String normalizeBibTeX(String input) {
if (input == null || input.isBlank()) {
return "";
Expand All @@ -181,7 +187,15 @@ private static String normalizeBibTeX(String input) {
return normalized;
}

// Helper method to ensure the Git repository is initialized
/**
* Ensures that a Git repository is initialized in the specified backup directory.
* If the `.git` directory does not exist, it initializes a new Git repository.
* Then, it builds the Git object for further operations.
*
* @param backupDir The backup directory containing the Git repository.
* @throws IOException If an I/O error occurs.
* @throws GitAPIException If a Git API error occurs.
*/
static void ensureGitInitialized(Path backupDir) throws IOException, GitAPIException {

// This method was created because the initialization of the Git object, when written in the constructor, was causing a NullPointerException
Expand Down Expand Up @@ -210,17 +224,22 @@ static void ensureGitInitialized(Path backupDir) throws IOException, GitAPIExcep
git = new Git(repository);
}

// Helper method to copy the database file to the backup directory
protected static void copyDatabaseFileToBackupDir(Path dbFile, Path backupDirPath) throws IOException {
/**
* Copies the database file to the backup directory with a unique file name.
* The unique file name is generated by appending a UUID to the original file name.
*
* @param dbFile The path to the database file to be copied.
* @param backupDirPath The path to the backup directory.
* @throws IOException If an I/O error occurs during the file copy operation.
*/
protected static void copyDatabaseFileToBackupDir(Path dbFile, Path backupDirPath) throws IOException {
String fileUuid = getOrGenerateFileUuid(dbFile);
String uniqueFileName = appendUuidToFileName(dbFile.getFileName().toString(), fileUuid);
Path backupFilePath = backupDirPath.resolve(uniqueFileName);
Files.copy(dbFile, backupFilePath, StandardCopyOption.REPLACE_EXISTING);
LOGGER.info("Database file uniquely copied to backup directory: {}", backupFilePath);
}

// A method

/**
* Starts a new BackupManager instance and begins the backup task.
*
Expand All @@ -232,7 +251,6 @@ protected static void copyDatabaseFileToBackupDir(Path dbFile, Path backupDirPat
* @throws IOException if an I/O error occurs
* @throws GitAPIException if a Git API error occurs
*/

public static BackupManager start(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, CliPreferences preferences) throws IOException, GitAPIException {
LOGGER.info("In methode Start");
Path backupDir = preferences.getFilePreferences().getBackupDirectory();
Expand Down Expand Up @@ -265,7 +283,6 @@ public static void shutdown(BibDatabaseContext bibDatabaseContext, Path backupDi
*
* @param backupDir the backup directory
*/

void startBackupTask(Path backupDir, BibDatabaseContext bibDatabaseContext) {
LOGGER.info("Initializing backup task for directory: {} and file: {}", backupDir, bibDatabaseContext.getDatabasePath().orElseThrow());
executor.scheduleAtFixedRate(
Expand All @@ -292,21 +309,20 @@ void startBackupTask(Path backupDir, BibDatabaseContext bibDatabaseContext) {
* @throws IOException if an I/O error occurs
* @throws GitAPIException if a Git API error occurs
*/

protected void performBackup(Path dbfile, Path backupDir) throws IOException, GitAPIException {

boolean needsCommit = backupGitDiffers(dbfile, backupDir);

if (!needsBackup && !needsCommit) {
LOGGER.info("No changes detected, beacuse needsBackup is :{} and needsCommit is :{}", needsBackup, needsCommit);
LOGGER.info("No changes detected, beacuse needsBackup is: {} and needsCommit is: {}", needsBackup, needsCommit);
return;
}

if (needsBackup) {
LOGGER.info("Backup needed, because needsBackup is :{}", needsBackup);
LOGGER.info("Backup needed, because needsBackup is: {}", needsBackup);
} else {
LOGGER.info("Backup needed, because needsCommit is :{}", needsCommit);
}
LOGGER.info("Backup needed, because needsCommit is: {}", needsCommit);
}

// Stage the file for commit
git.add().addFilepattern(".").call();
Expand All @@ -316,9 +332,14 @@ protected void performBackup(Path dbfile, Path backupDir) throws IOException, Gi
RevCommit commit = git.commit()
.setMessage("Backup at " + Instant.now().toString())
.call();
LOGGER.info("Backup committed in :{} with commit ID: {} for the file : {}", backupDir, commit.getName(), bibDatabaseContext.getDatabasePath().orElseThrow());
LOGGER.info("Backup committed in: {} with commit ID: {} for the file: {}", backupDir, commit.getName(), bibDatabaseContext.getDatabasePath().orElseThrow());
}

/**
* Listens for changes in the BibDatabaseContext and sets the needsBackup flag if a change is detected.
*
* @param event The BibDatabaseContextChangedEvent to listen for.
*/
public synchronized void listen(BibDatabaseContextChangedEvent event) {
if (!event.isFilteredOut()) {
LOGGER.info("Change detected/LISTENED in file: {}", bibDatabaseContext.getDatabasePath().orElseThrow());
Expand All @@ -327,12 +348,14 @@ public synchronized void listen(BibDatabaseContextChangedEvent event) {
}

/**
* Restores the backup from the specified commit.
* Restores the backup of the specified database file from the given commit ID.
* This method retrieves the content of the file from the specified commit in the Git repository
* and rewrites the original file with the retrieved content.
*
* @param backupDir the backup directory
* @param objectId the commit ID to restore from
* @param dbFile The path to the database file to be restored.
* @param backupDir The backup directory containing the Git repository.
* @param objectId The commit ID from which to restore the file.
*/

public static void restoreBackup(Path dbFile, Path backupDir, ObjectId objectId) {
try (Repository repository = openGitRepository(backupDir)) {
// Resolve the filename of dbFile in the repository
Expand All @@ -345,7 +368,7 @@ public static void restoreBackup(Path dbFile, Path backupDir, ObjectId objectId)
LOGGER.info("Restoring file: {}", gitPath);

// Load the content of the file from the specified commit
ObjectId fileObjectId = repository.resolve(objectId.getName() + ":" + gitPath);
ObjectId fileObjectId = repository.resolve(objectId.getName() + ": " + gitPath);
if (fileObjectId == null) { // File not found in the commit
performBackupNoCommits(dbFile, backupDir);
}
Expand All @@ -363,14 +386,16 @@ public static void restoreBackup(Path dbFile, Path backupDir, ObjectId objectId)
}

/**
* Checks if there are differences between the files in the directory and the last commit.
* Checks if there are differences between the current database file and the last committed version in the Git repository.
* This method ensures the database file is copied to the backup directory, initializes the Git repository if needed,
* and compares the content of the file in the repository with the current file.
*
* @param backupDir the backup directory
* @return true if there are differences, false otherwise
* @throws IOException if an I/O error occurs
* @throws GitAPIException if a Git API error occurs
* @param dbFile The path to the database file to be checked.
* @param backupDir The backup directory containing the Git repository.
* @return true if there are differences, false otherwise.
* @throws IOException If an I/O error occurs.
* @throws GitAPIException If a Git API error occurs.
*/

public static boolean backupGitDiffers(Path dbFile, Path backupDir) throws IOException, GitAPIException {

// Ensure the specific database file is copied to the backup directory
Expand Down Expand Up @@ -429,6 +454,14 @@ public static boolean backupGitDiffers(Path dbFile, Path backupDir) throws IOExc
return false; // No differences found
}

/**
* Retrieves the backup file path for the given database file.
* This method generates or retrieves a UUID for the file and constructs the backup file path using the UUID.
*
* @param dbFile The path to the database file.
* @param backupDir The backup directory.
* @return The path to the backup file.
*/
public static Path getBackupFilePath(Path dbFile, Path backupDir) {
try {
String baseName = dbFile.getFileName().toString();
Expand All @@ -440,6 +473,15 @@ public static Path getBackupFilePath(Path dbFile, Path backupDir) {
}
}

/**
* Writes the backup file content from the specified commit ID to the backup directory.
* This method retrieves the content of the file from the specified commit in the Git repository
* and rewrites the backup file with the retrieved content.
*
* @param dbFile The path to the database file.
* @param backupDir The backup directory containing the Git repository.
* @param objectId The commit ID from which to retrieve the file content.
*/
public static void writeBackupFileToCommit(Path dbFile, Path backupDir, ObjectId objectId) {
try (Repository repository = openGitRepository(backupDir)) {
// Resolve the filename of dbFile in the repository
Expand All @@ -452,7 +494,7 @@ public static void writeBackupFileToCommit(Path dbFile, Path backupDir, ObjectId
LOGGER.info("Restoring file: {}", gitPath);

// Load the content of the file from the specified commit
ObjectId fileObjectId = repository.resolve(objectId.getName() + ":" + gitPath);
ObjectId fileObjectId = repository.resolve(objectId.getName() + ": " + gitPath);
if (fileObjectId == null) { // File not found in the commit
performBackupNoCommits(dbFile, backupDir);
}
Expand All @@ -470,6 +512,13 @@ public static void writeBackupFileToCommit(Path dbFile, Path backupDir, ObjectId
}
}

/**
* Opens the Git repository located in the specified backup directory.
*
* @param backupDir The backup directory containing the Git repository.
* @return The opened Git repository.
* @throws IOException If an I/O error occurs.
*/
private static Repository openGitRepository(Path backupDir) throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
// Initialize Git repository from the backup directory
Expand All @@ -489,7 +538,6 @@ private static Repository openGitRepository(Path backupDir) throws IOException {
* @throws IOException if an I/O error occurs
* @throws GitAPIException if a Git API error occurs
*/

public List<DiffEntry> showDiffers(Path dbFile, Path backupDir, String commitId) throws IOException, GitAPIException {

File repoDir = backupDir.toFile();
Expand Down Expand Up @@ -519,7 +567,6 @@ need a class to show the last ten backups indicating: date/ size/ number of entr
* @throws IOException if an I/O error occurs
* @throws GitAPIException if a Git API error occurs
*/

public static List<RevCommit> retrieveCommits(Path dbFile, Path backupDir, int n) throws IOException, GitAPIException {
List<RevCommit> retrievedCommits = new ArrayList<>();

Expand Down Expand Up @@ -635,6 +682,15 @@ public static List<BackupEntry> retrieveCommitDetails(List<RevCommit> commits, P
return commitDetails;
}

/**
* Performs the initial backup by creating the first commit in the Git repository.
* This method ensures the Git repository is initialized, stages the database file, and commits it.
*
* @param dbFile The path to the database file to be backed up.
* @param backupDir The backup directory containing the Git repository.
* @throws IOException If an I/O error occurs.
* @throws GitAPIException If a Git API error occurs.
*/
public static void performBackupNoCommits(Path dbFile, Path backupDir) throws IOException, GitAPIException {

LOGGER.info("No commits found in the repository. We need a first commit.");
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/gui/backup/BackupChoiceDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public BackupChoiceDialog(Path backupDir, List<BackupEntry> backups) {
getDialogPane().setMinWidth(450);
getDialogPane().getButtonTypes().setAll(RESTORE_BACKUP, IGNORE_BACKUP, REVIEW_BACKUP);

String content = Localization.lang("It looks like JabRef did not shut down cleanly last time the file was used.") + "\n\n" +
Localization.lang("Do you want to recover the library from a backup file?");

String content = Localization.lang("It looks like JabRef did not shut down cleanly last time the file was used.\n\nDo you want to recover the library from a backup file?");
backupTableView = new TableView<>();
setupBackupTableView();
tableData.addAll(backups);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/csl-styles
Submodule csl-styles updated 128 files

0 comments on commit 7477e94

Please sign in to comment.