# Remove ProjectID from .Rproj files if freshly added
local({
  xfun <- requireNamespace("xfun", quietly = TRUE)
  rproj_files <- list.files(pattern = "\\.Rproj$", full.names = TRUE)
  if (!xfun || length(rproj_files) == 0L) return(invisible(NULL))

  lapply(rproj_files, function(f) {
    diff_cmd <- system(paste("git diff --", shQuote(f)), intern = TRUE)
    diff_out <- tryCatch(diff_cmd, error = function(e) character(0))

    if (any(grepl("^\\+ProjectId: ", diff_out))) {
      dcf_old <- xfun::read_utf8(f)
      dcf_new <- dcf_old[!grepl("^ProjectId: ", dcf_old)]
      xfun::write_utf8(dcf_new, f)
    }
  })

  invisible(NULL)
})