From 050dbc91ebb9e1c72dadfbf9126963144e89452f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 7 Dec 2023 10:28:24 +0100 Subject: [PATCH] some refactoring for clarity --- R/update.R | 56 ++++++++++--------- ...2-POST.json => translate-996cd0-POST.json} | 2 +- ...0-POST.json => translate-d45a92-POST.json} | 2 +- 3 files changed, 32 insertions(+), 28 deletions(-) rename tests/testthat/fixtures/git2/api/{translate-e96072-POST.json => translate-996cd0-POST.json} (55%) rename tests/testthat/fixtures/git2/api/{translate-b3fa90-POST.json => translate-d45a92-POST.json} (54%) diff --git a/R/update.R b/R/update.R index cc03fab..b238261 100644 --- a/R/update.R +++ b/R/update.R @@ -1,6 +1,14 @@ #' Update a translation of a file in a Git repo #' #' Re-use existing translation where possible +#' (at the node level: paragraph, heading, etc.) +#' +#' @details +#' The function looks for the latest commit that updated the source file, +#' and for the latest commit that updated the target file. +#' If the target file was updated last, nothing happens: you might need to +#' reorder the Git history with rebase for instance. +#' #' #' @inheritParams deepl_translate #' @@ -9,12 +17,12 @@ #' #' deepl_update <- function(path, - out_path, - yaml_fields = c("title", "description"), - glossary_name = NULL, - source_lang = NULL, - target_lang = NULL, - formality = c("default", "more", "less", "prefer_more", "prefer_less")) { + out_path, + yaml_fields = c("title", "description"), + glossary_name = NULL, + source_lang = NULL, + target_lang = NULL, + formality = c("default", "more", "less", "prefer_more", "prefer_less")) { rlang::check_installed("rprojroot") @@ -43,44 +51,40 @@ deepl_update <- function(path, log <- gert::git_log(repo = repo) found_source <- FALSE - i <- 0 + latest_source_commit_index <- 0 while (!found_source) { - i <- i + 1 - diff_info <- gert::git_diff(log[["commit"]][[i]], repo = repo) + latest_source_commit_index <- latest_source_commit_index + 1 + diff_info <- gert::git_diff(log[["commit"]][[latest_source_commit_index]], repo = repo) # TODO or not, won't work if it was renamed in the important timeframe found_source <- (fs::path_file(path) %in% diff_info[["new"]]) } found_target <- FALSE - j <- 0 + latest_target_commit_index <- 0 while (!found_target) { - j <- j + 1 - diff_info <- gert::git_diff(log[["commit"]][[j]], repo = repo) + latest_target_commit_index <- latest_target_commit_index + 1 + diff_info <- gert::git_diff(log[["commit"]][[latest_target_commit_index]], repo = repo) # TODO or not, won't work if it was renamed in the important timeframe found_target <- (fs::path_file(out_path) %in% diff_info[["new"]]) } - if (i >= j) { + if (latest_source_commit_index >= latest_target_commit_index) { return(NULL) } - # need to find the diff between file original as of j commit - dirj <- withr::local_tempdir() - fs::dir_copy(repo, dirj, overwrite = TRUE) - gert::git_reset_hard(ref = log[["commit"]][[j]], repo = dirj) + dir_at_target_latest_update <- withr::local_tempdir() + fs::dir_copy(repo, dir_at_target_latest_update, overwrite = TRUE) + gert::git_reset_hard( + ref = log[["commit"]][[latest_target_commit_index]], + repo = dir_at_target_latest_update + ) old_source <- tinkr::yarn$new( - file.path(dirj, fs::path_file(path)), - sourcepos = TRUE - ) - new_source <- tinkr::yarn$new( - file.path(repo, fs::path_file(path)), - sourcepos = TRUE - ) - old_target <- tinkr::yarn$new( - file.path(out_path), + file.path(dir_at_target_latest_update, fs::path_file(path)), sourcepos = TRUE ) + new_source <- tinkr::yarn$new(file.path(repo, fs::path_file(path))) + old_target <- tinkr::yarn$new(file.path(out_path)) same_structure <- (xml2::xml_length(old_source$body) == xml2::xml_length(old_target$body)) && diff --git a/tests/testthat/fixtures/git2/api/translate-e96072-POST.json b/tests/testthat/fixtures/git2/api/translate-996cd0-POST.json similarity index 55% rename from tests/testthat/fixtures/git2/api/translate-e96072-POST.json rename to tests/testthat/fixtures/git2/api/translate-996cd0-POST.json index 3232811..92cc832 100644 --- a/tests/testthat/fixtures/git2/api/translate-e96072-POST.json +++ b/tests/testthat/fixtures/git2/api/translate-996cd0-POST.json @@ -2,7 +2,7 @@ "translations": [ { "detected_source_language": "EN", - "text": "\n\n\n \n impresionante<\/text>\n <\/paragraph>\n<\/document>\n" + "text": "\n\n\n \n un título<\/text>\n <\/heading>\n<\/document>\n" } ] } diff --git a/tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json b/tests/testthat/fixtures/git2/api/translate-d45a92-POST.json similarity index 54% rename from tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json rename to tests/testthat/fixtures/git2/api/translate-d45a92-POST.json index 4e7c92f..254add2 100644 --- a/tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json +++ b/tests/testthat/fixtures/git2/api/translate-d45a92-POST.json @@ -2,7 +2,7 @@ "translations": [ { "detected_source_language": "EN", - "text": "\n\n\n \n un título<\/text>\n <\/heading>\n<\/document>\n" + "text": "\n\n\n \n impresionante<\/text>\n <\/paragraph>\n<\/document>\n" } ] }