Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c196941b
Commit
c196941b
authored
May 27, 2021
by
Thomas Randolph
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a utility to compute stats for a diff file
parent
3e86f62f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
1 deletion
+89
-1
app/assets/javascripts/diffs/utils/diff_file.js
app/assets/javascripts/diffs/utils/diff_file.js
+33
-0
spec/frontend/diffs/mock_data/diff_file.js
spec/frontend/diffs/mock_data/diff_file.js
+2
-0
spec/frontend/diffs/utils/diff_file_spec.js
spec/frontend/diffs/utils/diff_file_spec.js
+54
-1
No files found.
app/assets/javascripts/diffs/utils/diff_file.js
View file @
c196941b
import
{
diffViewerModes
as
viewerModes
}
from
'
~/ide/constants
'
;
import
{
changeInPercent
,
numberToHumanSize
}
from
'
~/lib/utils/number_utils
'
;
import
{
truncateSha
}
from
'
~/lib/utils/text_utility
'
;
import
{
uuids
}
from
'
~/lib/utils/uuids
'
;
...
...
@@ -87,3 +88,35 @@ export function isCollapsed(file) {
export
function
getShortShaFromFile
(
file
)
{
return
file
.
content_sha
?
truncateSha
(
String
(
file
.
content_sha
))
:
null
;
}
export
function
stats
(
file
)
{
let
valid
=
false
;
let
classes
=
''
;
let
sign
=
''
;
let
text
=
''
;
let
percent
=
0
;
let
diff
=
0
;
if
(
file
)
{
percent
=
changeInPercent
(
file
.
old_size
,
file
.
new_size
);
diff
=
file
.
new_size
-
file
.
old_size
;
sign
=
diff
>=
0
?
'
+
'
:
''
;
text
=
`
${
sign
}${
numberToHumanSize
(
diff
)}
(
${
sign
}${
percent
}
%)`
;
valid
=
true
;
if
(
diff
>
0
)
{
classes
=
'
cgreen
'
;
}
else
if
(
diff
<
0
)
{
classes
=
'
cred
'
;
}
}
return
{
changed
:
diff
,
text
,
percent
,
classes
,
sign
,
valid
,
};
}
spec/frontend/diffs/mock_data/diff_file.js
View file @
c196941b
...
...
@@ -19,6 +19,8 @@ export default {
renamed_file
:
false
,
old_path
:
'
CHANGELOG
'
,
new_path
:
'
CHANGELOG
'
,
old_size
:
1024
,
new_size
:
2048
,
mode_changed
:
false
,
a_mode
:
'
100644
'
,
b_mode
:
'
100644
'
,
...
...
spec/frontend/diffs/utils/diff_file_spec.js
View file @
c196941b
import
{
prepareRawDiffFile
,
getShortShaFromFile
,
isNotDiffable
}
from
'
~/diffs/utils/diff_file
'
;
import
{
prepareRawDiffFile
,
getShortShaFromFile
,
stats
,
isNotDiffable
,
}
from
'
~/diffs/utils/diff_file
'
;
import
{
diffViewerModes
}
from
'
~/ide/constants
'
;
import
mockDiffFile
from
'
../mock_data/diff_file
'
;
function
getDiffFiles
()
{
const
loadFull
=
'
namespace/project/-/merge_requests/12345/diff_for_path?file_identifier=abc
'
;
...
...
@@ -156,6 +162,53 @@ describe('diff_file utilities', () => {
});
});
describe
(
'
stats
'
,
()
=>
{
const
noFile
=
[
"
returns empty stats when the file isn't provided
"
,
undefined
,
{
text
:
''
,
percent
:
0
,
changed
:
0
,
classes
:
''
,
sign
:
''
,
valid
:
false
,
},
];
const
validFile
=
[
'
computes the correct stats from a file
'
,
mockDiffFile
,
{
changed
:
1024
,
percent
:
100
,
classes
:
'
cgreen
'
,
sign
:
'
+
'
,
text
:
'
+1.00 KiB (+100%)
'
,
valid
:
true
,
},
];
const
negativeChange
=
[
'
computed the correct states from a file with a negative size change
'
,
{
...
mockDiffFile
,
new_size
:
0
,
old_size
:
1024
,
},
{
changed
:
-
1024
,
percent
:
-
100
,
classes
:
'
cred
'
,
sign
:
''
,
text
:
'
-1.00 KiB (-100%)
'
,
valid
:
true
,
},
];
it
.
each
([
noFile
,
validFile
,
negativeChange
])(
'
%s
'
,
(
_
,
file
,
output
)
=>
{
expect
(
stats
(
file
)).
toEqual
(
output
);
});
});
describe
(
'
isNotDiffable
'
,
()
=>
{
it
.
each
`
bool | vw
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment