Skip to content

Commit

Permalink
make structuredDiffForFile for BitBucketServer the same as the others
Browse files Browse the repository at this point in the history
  • Loading branch information
berlysia committed Jan 24, 2022
1 parent a7355a3 commit 0b5865f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
4 changes: 3 additions & 1 deletion source/platforms/BitBucketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ export class BitBucketServer implements Platform {
let change
for (let chunk of diff!.chunks) {
// Search for a change (that is not a deletion) and with given line. We want to look only for destination lines of a change
change = chunk.changes.find((c: any) => c.type != "del" && c.destinationLine == line)
change = chunk.changes.find(
c => (c.type === "normal" && c.ln2 === line) || (c.type === "add" && c.ln === line)
)
break
}
if (change === undefined) {
Expand Down
55 changes: 44 additions & 11 deletions source/platforms/bitbucket_server/BitBucketServerGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GitCommit } from "../../dsl/Commit"

import { BitBucketServerAPI } from "./BitBucketServerAPI"

import { GitJSONToGitDSLConfig, gitJSONToGitDSL, GitStructuredDiff } from "../git/gitJSONToGitDSL"
import { GitJSONToGitDSLConfig, gitJSONToGitDSL, GitStructuredDiff, Changes } from "../git/gitJSONToGitDSL"

import { debug } from "../../debug"
const d = debug("BitBucketServerGit")
Expand Down Expand Up @@ -131,23 +131,56 @@ const bitBucketServerChangesToGitJSONDSL = (

const bitBucketServerDiffToGitStructuredDiff = (diffs: BitBucketServerDiff[]): GitStructuredDiff => {
// We need all changed lines with it's type. It will convert hunk segment lines to flatten changed lines.
const segmentValues = { ADDED: "add", CONTEXT: "normal", REMOVED: "del" }
const segmentValues = { ADDED: "add", CONTEXT: "normal", REMOVED: "del" } as const
return diffs.map(diff => ({
from: diff.source && diff.source.toString,
to: diff.destination && diff.destination.toString,
chunks:
diff.hunks &&
diff.hunks.map(hunk => ({
content: `@@ -${hunk.sourceLine},${hunk.sourceSpan} +${hunk.destinationLine},${hunk.destinationSpan} @@`,
oldStart: hunk.sourceLine,
oldLines: hunk.sourceSpan,
newStart: hunk.destinationLine,
newLines: hunk.destinationSpan,
changes: hunk.segments
.map(segment =>
segment.lines.map(line => ({
type: segmentValues[segment.type] as "add" | "del" | "normal",
content: line.line,
sourceLine: line.source,
destinationLine: line.destination,
}))
)
.reduce((a, b) => a.concat(b), []),
.map(segment => {
const type = segmentValues[segment.type]
if (type === "add") {
return segment.lines.map(line => {
return {
type,
add: true as const,
content: line.line,
ln: line.destination,
}
})
}
if (type === "del") {
return segment.lines.map(line => {
return {
type,
del: true as const,
content: line.line,
ln: line.source,
}
})
}
if (type === "normal") {
return segment.lines.map(line => {
return {
type,
normal: true as const,
content: line.line,
ln1: line.source,
ln2: line.destination,
}
})
}
// unknown type: ${type}
return []
})
.reduce((a, b) => a.concat(b), [] as Changes),
})),
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,71 +38,86 @@ Array [
"changes": Array [
Object {
"content": "node_modules",
"destinationLine": 1,
"sourceLine": 1,
"ln1": 1,
"ln2": 1,
"normal": true,
"type": "normal",
},
Object {
"content": "*.log",
"destinationLine": 2,
"sourceLine": 2,
"ln1": 2,
"ln2": 2,
"normal": true,
"type": "normal",
},
Object {
"content": "/test.py",
"destinationLine": 3,
"sourceLine": 3,
"ln1": 3,
"ln2": 3,
"normal": true,
"type": "normal",
},
Object {
"content": "/.vscode",
"destinationLine": 4,
"sourceLine": 4,
"ln1": 4,
"ln2": 4,
"normal": true,
"type": "normal",
},
Object {
"content": ".DS_Store",
"destinationLine": 5,
"sourceLine": 5,
"ln1": 5,
"ln2": 5,
"normal": true,
"type": "normal",
},
Object {
"content": "coverage",
"destinationLine": 6,
"sourceLine": 6,
"ln1": 6,
"ln2": 6,
"normal": true,
"type": "normal",
},
Object {
"content": ".idea",
"destinationLine": 7,
"sourceLine": 7,
"ln1": 7,
"ln2": 7,
"normal": true,
"type": "normal",
},
Object {
"content": "__pycache__/",
"destinationLine": 8,
"sourceLine": 8,
"ln1": 8,
"ln2": 8,
"normal": true,
"type": "normal",
},
Object {
"content": "*.pyc",
"destinationLine": 9,
"sourceLine": 9,
"ln1": 9,
"ln2": 9,
"normal": true,
"type": "normal",
},
Object {
"add": true,
"content": "",
"destinationLine": 10,
"sourceLine": 10,
"ln": 10,
"type": "add",
},
Object {
"content": "*.swp",
"destinationLine": 11,
"sourceLine": 10,
"ln1": 10,
"ln2": 11,
"normal": true,
"type": "normal",
},
],
"content": "@@ -1,10 +1,11 @@",
"newLines": 11,
"newStart": 1,
"oldLines": 10,
"oldStart": 1,
},
]
`;

0 comments on commit 0b5865f

Please sign in to comment.