Skip to content

Commit

Permalink
Merge pull request desktop#17467 from ssigwart/renameChanged2
Browse files Browse the repository at this point in the history
Fix staged file renamed with changes showing no diff
  • Loading branch information
tidy-dev authored Dec 5, 2024
2 parents e02fb6d + e1ea97c commit 9d14ae6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
28 changes: 24 additions & 4 deletions app/src/lib/git/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,40 @@ function mapStatus(
return { kind: AppFileStatusKind.Deleted, submoduleStatus }
} // deleted
if (status === 'R' && oldPath != null) {
return { kind: AppFileStatusKind.Renamed, oldPath, submoduleStatus }
return {
kind: AppFileStatusKind.Renamed,
oldPath,
submoduleStatus,
renameIncludesModifications: false,
}
} // renamed
if (status === 'C' && oldPath != null) {
return { kind: AppFileStatusKind.Copied, oldPath, submoduleStatus }
return {
kind: AppFileStatusKind.Copied,
oldPath,
submoduleStatus,
renameIncludesModifications: false,
}
} // copied

// git log -M --name-status will return a RXXX - where XXX is a percentage
if (status.match(/R[0-9]+/) && oldPath != null) {
return { kind: AppFileStatusKind.Renamed, oldPath, submoduleStatus }
return {
kind: AppFileStatusKind.Renamed,
oldPath,
submoduleStatus,
renameIncludesModifications: status !== 'R100',
}
}

// git log -C --name-status will return a CXXX - where XXX is a percentage
if (status.match(/C[0-9]+/) && oldPath != null) {
return { kind: AppFileStatusKind.Copied, oldPath, submoduleStatus }
return {
kind: AppFileStatusKind.Copied,
oldPath,
submoduleStatus,
renameIncludesModifications: false,
}
}

return { kind: AppFileStatusKind.Modified, submoduleStatus }
Expand Down
11 changes: 10 additions & 1 deletion app/src/lib/git/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,17 @@ function convertToAppStatus(
kind: AppFileStatusKind.Copied,
oldPath,
submoduleStatus: entry.submoduleStatus,
renameIncludesModifications: false,
}
} else if (entry.kind === 'renamed' && oldPath != null) {
return {
kind: AppFileStatusKind.Renamed,
oldPath,
submoduleStatus: entry.submoduleStatus,
renameIncludesModifications:
entry.workingTree === GitStatusEntry.Modified ||
(entry.renameOrCopyScore !== undefined &&
entry.renameOrCopyScore < 100),
}
} else if (entry.kind === 'untracked') {
return {
Expand Down Expand Up @@ -276,7 +281,11 @@ function buildStatusMap(
entry: IStatusEntry,
conflictDetails: ConflictFilesDetails
): Map<string, WorkingDirectoryFileChange> {
const status = mapStatus(entry.statusCode, entry.submoduleStatusCode)
const status = mapStatus(
entry.statusCode,
entry.submoduleStatusCode,
entry.renameOrCopyScore
)

if (status.kind === 'ordinary') {
// when a file is added in the index but then removed in the working
Expand Down
11 changes: 10 additions & 1 deletion app/src/lib/status-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface IStatusEntry {

/** The original path in the case of a renamed file */
readonly oldPath?: string

/** The rename or copy score in the case of a renamed file */
readonly renameOrCopyScore?: number
}

export function isStatusHeader(
Expand Down Expand Up @@ -141,6 +144,7 @@ function parsedRenamedOrCopiedEntry(
statusCode: match[1],
submoduleStatusCode: match[2],
oldPath,
renameOrCopyScore: parseInt(match[8].substring(1), 10),
path: match[9],
}
}
Expand Down Expand Up @@ -196,7 +200,8 @@ function mapSubmoduleStatus(
*/
export function mapStatus(
statusCode: string,
submoduleStatusCode: string
submoduleStatusCode: string,
renameOrCopyScore: number | undefined
): FileEntry {
const submoduleStatus = mapSubmoduleStatus(submoduleStatusCode)

Expand Down Expand Up @@ -269,6 +274,7 @@ export function mapStatus(
kind: 'renamed',
index: GitStatusEntry.Renamed,
workingTree: GitStatusEntry.Unchanged,
renameOrCopyScore,
submoduleStatus,
}
}
Expand All @@ -278,6 +284,7 @@ export function mapStatus(
kind: 'renamed',
index: GitStatusEntry.Unchanged,
workingTree: GitStatusEntry.Renamed,
renameOrCopyScore,
submoduleStatus,
}
}
Expand Down Expand Up @@ -325,6 +332,7 @@ export function mapStatus(
kind: 'renamed',
index: GitStatusEntry.Renamed,
workingTree: GitStatusEntry.Modified,
renameOrCopyScore,
submoduleStatus,
}
}
Expand All @@ -334,6 +342,7 @@ export function mapStatus(
kind: 'renamed',
index: GitStatusEntry.Renamed,
workingTree: GitStatusEntry.Deleted,
renameOrCopyScore,
submoduleStatus,
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/models/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type PlainFileStatus = {
export type CopiedOrRenamedFileStatus = {
kind: AppFileStatusKind.Copied | AppFileStatusKind.Renamed
oldPath: string
renameIncludesModifications: boolean
submoduleStatus?: SubmoduleStatus
}

Expand Down Expand Up @@ -146,6 +147,8 @@ type RenamedOrCopiedEntry = {
readonly workingTree?: GitStatusEntry
/** the submodule status for this entry */
readonly submoduleStatus?: SubmoduleStatus
/** The rename or copy score in the case of a renamed file */
readonly renameOrCopyScore?: number
}

export enum UnmergedEntrySummary {
Expand Down
11 changes: 11 additions & 0 deletions app/src/ui/diff/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { BinaryFile } from './binary-file'
import { SideBySideDiff } from './side-by-side-diff'
import { IFileContents } from './syntax-highlighting'
import { SubmoduleDiff } from './submodule-diff'
import { Octicon } from '../octicons'
import * as OcticonSymbol from '../octicons/octicons.generated'

// image used when no diff is displayed
const NoDiffImage = encodePathAsUrl(__dirname, 'static/ufo-alert.svg')
Expand Down Expand Up @@ -224,6 +226,15 @@ export class Diff extends React.Component<IDiffProps, IDiffState> {
}

if (this.props.file.status.kind === AppFileStatusKind.Renamed) {
// Check if it was changed too
if (this.props.file.status.renameIncludesModifications) {
return (
<div className="panel renamed">
<Octicon symbol={OcticonSymbol.alert} />
The file was renamed and includes changes.
</div>
)
}
return (
<div className="panel renamed">
The file was renamed but not changed
Expand Down
8 changes: 8 additions & 0 deletions app/test/unit/git/log-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ describe('git/log', () => {
expect(first.files[0].status).toEqual({
kind: AppFileStatusKind.Renamed,
oldPath: 'NEW.md',
renameIncludesModifications: true,
submoduleStatus: undefined,
})

const second = await getChangedFiles(repository, 'c898ca8')
Expand All @@ -92,6 +94,8 @@ describe('git/log', () => {
expect(second.files[0].status).toEqual({
kind: AppFileStatusKind.Renamed,
oldPath: 'OLD.md',
renameIncludesModifications: false,
submoduleStatus: undefined,
})
})

Expand All @@ -111,12 +115,16 @@ describe('git/log', () => {
expect(changesetData.files[0].status).toEqual({
kind: AppFileStatusKind.Copied,
oldPath: 'initial.md',
renameIncludesModifications: false,
submoduleStatus: undefined,
})

expect(changesetData.files[1].path).toBe('duplicate.md')
expect(changesetData.files[1].status).toEqual({
kind: AppFileStatusKind.Copied,
oldPath: 'initial.md',
renameIncludesModifications: false,
submoduleStatus: undefined,
})
})

Expand Down
4 changes: 4 additions & 0 deletions app/test/unit/git/status-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ describe('git/status', () => {
expect(files[0].status).toEqual({
kind: AppFileStatusKind.Renamed,
oldPath: 'foo',
renameIncludesModifications: false,
submoduleStatus: undefined,
})
})

Expand Down Expand Up @@ -275,6 +277,8 @@ describe('git/status', () => {
expect(files[1].status).toEqual({
kind: AppFileStatusKind.Copied,
oldPath: 'CONTRIBUTING.md',
renameIncludesModifications: false,
submoduleStatus: undefined,
})
})

Expand Down

0 comments on commit 9d14ae6

Please sign in to comment.