Skip to content

Commit

Permalink
git storage: handle file renames between folders
Browse files Browse the repository at this point in the history
  • Loading branch information
EricFromCanada committed Jan 6, 2023
1 parent 54dbf9a commit 972a904
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/core/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module.exports = {
switch (WIKI.config.db.type) {
case 'postgres':
await conn.query(`set application_name = 'Wiki.js'`)
// -> Set schema if it's not public
// -> Set schema if it's not public
if (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') {
await conn.query(`set search_path TO ${WIKI.config.db.schema}, public;`)
}
Expand Down
3 changes: 2 additions & 1 deletion server/models/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ module.exports = class Page extends Model {
const destinationHash = pageHelper.generateHash({ path: opts.destinationPath, locale: opts.destinationLocale, privateNS: opts.isPrivate ? 'TODO' : '' })

// -> Move page
const destinationTitle = (page.title === page.path ? opts.destinationPath : page.title)
const destinationTitle = (page.title === _.last(page.path.split('/')) ? _.last(opts.destinationPath.split('/')) : page.title)
await WIKI.models.pages.query().patch({
path: opts.destinationPath,
localeCode: opts.destinationLocale,
Expand All @@ -745,6 +745,7 @@ module.exports = class Page extends Model {
...page,
destinationPath: opts.destinationPath,
destinationLocaleCode: opts.destinationLocale,
title: destinationTitle,
destinationHash
})

Expand Down
2 changes: 1 addition & 1 deletion server/modules/storage/disk/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = {
transform: async (page, enc, cb) => {
const pageObject = await WIKI.models.pages.query().findById(page.id)
page.tags = await pageObject.$relatedQuery('tags')

let fileName = `${page.path}.${pageHelper.getFileExtension(page.contentType)}`
if (WIKI.config.lang.code !== page.localeCode) {
fileName = `${page.localeCode}/${fileName}`
Expand Down
28 changes: 23 additions & 5 deletions server/modules/storage/git/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,28 @@ module.exports = {
const diff = await this.git.diffSummary(['-M', currentCommitLog.hash, latestCommitLog.hash])
if (_.get(diff, 'files', []).length > 0) {
let filesToProcess = []
const filePattern = /(.*?)(?:{(.*?))? => (?:(.*?)})?(.*)/
for (const f of diff.files) {
const fMoved = f.file.split(' => ')
const fName = fMoved.length === 2 ? fMoved[1] : fMoved[0]
const fPath = path.join(this.repoPath, fName)
const fMatch = f.file.match(filePattern)
const fNames = function () {
if (!fMatch) {
return {
"old":f.file,
"new":f.file
}
} else if (!fMatch[2] && !fMatch[3]) {
return {
"old":fMatch[1],
"new":fMatch[4]
}
} else {
return {
"old":(fMatch[1]+fMatch[2]+fMatch[4]).replace('//', '/'),
"new":(fMatch[1]+fMatch[3]+fMatch[4]).replace('//', '/')
}
}
}()
const fPath = path.join(this.repoPath, fNames.new)
let fStats = { size: 0 }
try {
fStats = await fs.stat(fPath)
Expand All @@ -165,8 +183,8 @@ module.exports = {
path: fPath,
stats: fStats
},
oldPath: fMoved[0],
relPath: fName
oldPath: fNames.old,
relPath: fNames.new
})
}
await this.processFiles(filesToProcess, rootUser)
Expand Down

0 comments on commit 972a904

Please sign in to comment.