Skip to content

Commit

Permalink
some refactoring for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Dec 7, 2023
1 parent 8779727 commit 050dbc9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
56 changes: 30 additions & 26 deletions R/update.R
Original file line number Diff line number Diff line change
@@ -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
#'
Expand All @@ -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")
Expand Down Expand Up @@ -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)) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"translations": [
{
"detected_source_language": "EN",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph sourcepos=\"5:1-5:7\">\n <text sourcepos=\"5:1-5:7\" xml:space=\"preserve\">impresionante<\/text>\n <\/paragraph>\n<\/document>\n"
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <heading level=\"1\">\n <text xml:space=\"preserve\">un título<\/text>\n <\/heading>\n<\/document>\n"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"translations": [
{
"detected_source_language": "EN",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <heading sourcepos=\"1:1-1:9\" level=\"1\">\n <text sourcepos=\"1:3-1:9\" xml:space=\"preserve\">un título<\/text>\n <\/heading>\n<\/document>\n"
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <text xml:space=\"preserve\">impresionante<\/text>\n <\/paragraph>\n<\/document>\n"
}
]
}

0 comments on commit 050dbc9

Please sign in to comment.