From d6066870cc38acde9a3608041e133ca64b6694c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kim=20Carlb=C3=A4cker?= <kim.carlbacker@gmail.com>
Date: Thu, 2 Nov 2017 08:29:03 +0000
Subject: [PATCH] Fix encoding issue with Repository.ls_files

---
 lib/gitlab/gitaly_client/commit_service.rb        |   2 +-
 spec/lib/gitlab/git/repository_spec.rb            |   5 +++++
 .../88/3e379fcaa5f818fca81cdbabd7a497794d6535     | Bin 0 -> 304 bytes
 .../c8/b1ab16c858c67b680eea4644cf652485f555cf     | Bin 0 -> 597 bytes
 .../e3/7697aea12699f0b44544332a7c0f41ace5fb16     |   2 ++
 .../eb/a0c153ed20d927bab00507f356043b6b4be31e     | Bin 0 -> 185 bytes
 .../f6/5ad228d96e2a2ae7088e8557fe8906f6dd2b3f     | Bin 0 -> 642 bytes
 7 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 spec/support/gitlab-git-test.git/objects/88/3e379fcaa5f818fca81cdbabd7a497794d6535
 create mode 100644 spec/support/gitlab-git-test.git/objects/c8/b1ab16c858c67b680eea4644cf652485f555cf
 create mode 100644 spec/support/gitlab-git-test.git/objects/e3/7697aea12699f0b44544332a7c0f41ace5fb16
 create mode 100644 spec/support/gitlab-git-test.git/objects/eb/a0c153ed20d927bab00507f356043b6b4be31e
 create mode 100644 spec/support/gitlab-git-test.git/objects/f6/5ad228d96e2a2ae7088e8557fe8906f6dd2b3f

diff --git a/lib/gitlab/gitaly_client/commit_service.rb b/lib/gitlab/gitaly_client/commit_service.rb
index a2b50f2507e..da5505cb2fe 100644
--- a/lib/gitlab/gitaly_client/commit_service.rb
+++ b/lib/gitlab/gitaly_client/commit_service.rb
@@ -18,7 +18,7 @@ module Gitlab
 
         response = GitalyClient.call(@repository.storage, :commit_service, :list_files, request)
         response.flat_map do |msg|
-          msg.paths.map { |d| d.dup.force_encoding(Encoding::UTF_8) }
+          msg.paths.map { |d| EncodingHelper.encode!(d.dup) }
         end
       end
 
diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb
index 0fa88d17f5d..75ff45ec379 100644
--- a/spec/lib/gitlab/git/repository_spec.rb
+++ b/spec/lib/gitlab/git/repository_spec.rb
@@ -1163,6 +1163,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
 
   describe "#ls_files" do
     let(:master_file_paths) { repository.ls_files("master") }
+    let(:utf8_file_paths) { repository.ls_files("ls-files-utf8") }
     let(:not_existed_branch) { repository.ls_files("not_existed_branch") }
 
     it "read every file paths of master branch" do
@@ -1184,6 +1185,10 @@ describe Gitlab::Git::Repository, seed_helper: true do
     it "returns empty array when not existed branch" do
       expect(not_existed_branch.length).to equal(0)
     end
+
+    it "returns valid utf-8 data" do
+      expect(utf8_file_paths.map { |file| file.force_encoding('utf-8') }).to all(be_valid_encoding)
+    end
   end
 
   describe "#copy_gitattributes" do
diff --git a/spec/support/gitlab-git-test.git/objects/88/3e379fcaa5f818fca81cdbabd7a497794d6535 b/spec/support/gitlab-git-test.git/objects/88/3e379fcaa5f818fca81cdbabd7a497794d6535
new file mode 100644
index 0000000000000000000000000000000000000000..1c47f34b9a5592530770a95b8e26029badb47851
GIT binary patch
literal 304
zcmV-00nh$;0V^p=O;s>9uwXDWFfcPQQP8l^ypxn#q!5spnOBmOn3JPdoLs^1Z0@7k
zn=W2+_U_hPR~~ia(376vi6#a>ppaUTnp|3vn3R)R%y8vT|3S<5lP~+6ES4~wP#P3v
zeuWL9Dx)MfhhgE1AJ<*av_D>{(j0NqRD1i+1?zS}q%(69(}DUYgbQnXu&R{5THUbv
z(32^Pv?}fIK@?;aGgJf%h-ZFw5?8%@M8xvYSvP^lRW~7$IcdcVEJqqYUJ{!m`O)Q(
z<USX6^P1iJS)j6+dD+Db(_S05s{EY6IX_E{CDd2+i51_mM2LKDVo`QVet90l-w2t+
z*<a`U{WYhl$-H{Q9R1Ex1&ETO(xgfT<yXFfOZMJOco(d2^mOXh&42eK+y(&C(Su2Z
C8kuAO

literal 0
HcmV?d00001

diff --git a/spec/support/gitlab-git-test.git/objects/c8/b1ab16c858c67b680eea4644cf652485f555cf b/spec/support/gitlab-git-test.git/objects/c8/b1ab16c858c67b680eea4644cf652485f555cf
new file mode 100644
index 0000000000000000000000000000000000000000..ca13c8df66a5f80e358ee2da0a9a74421e8d6a52
GIT binary patch
literal 597
zcmV-b0;>IZ0V^p=O;s>6HD@q1FfcPQQP4}zEXhpI%P&f0xWCGN`YGc&1yQdVYvyu4
z&%G;urU^+^ZhlH>PHHj3-J08<_jmDU{@5JB@lGRRuS$-)7F3zDhohgntB=1s!*R#b
z^%un}WcJK3OEI3sA2~aEvmZ>6zh6j@r&DN%r=PoCZVJP}inj2bkuw&3EMM{?H)9#U
zR)b(YRH=JvZdztes$NxQ0Yl&3lt^>A^o?e*#u^>U{c?_<&g(#x`FJ|J`USf(Y+iLa
zY_opfY|#SIo|n@&ibc7Po`ov$b@cQLarJZbb9M#UIrZj;rO$V>`pvyOJ+yPj{bQYv
z_&T5}1A_dWU4w%`s`i%_s2<@-diH<WM_CKW<C)8I_i;j11-Ux9_=1i4wO+F8^Wxn7
zj?+Za3TzHusY$#L2vrp38Wim5@5eB+oqdbqiP<+7vl$!z{I2od&iNvvi2)EOBxU9?
ztnix3v9#G~pU#q#HmS--e(sHbc^4v^lA2nOTEwuZX_70$1ZE5IYbw_)c;uu1KYr*B
zQIMLKoS%}Jm(CE-^upI~uk-<tkWIn);gReDcR4;nlz_son4!bYeEzAWKP3LFkh#73
z`jY9DzNw~$W?&}(<3A@cNjJSHF*%zdB|gWe>92hp+qXg!SFUjG%DB*FsOpL{Qgd<`
zD)+p4HfL)oZ)*A`<;cpJ`&Y_r-U+cUFSWQNHHD$#fttNq)<Lnu(`Ko+UJqoy{ie_!
jYEW@z1%tzU^CKT3QlI=*=qVRz{Uo?Z$43_cRKNd1OV>6Q

literal 0
HcmV?d00001

diff --git a/spec/support/gitlab-git-test.git/objects/e3/7697aea12699f0b44544332a7c0f41ace5fb16 b/spec/support/gitlab-git-test.git/objects/e3/7697aea12699f0b44544332a7c0f41ace5fb16
new file mode 100644
index 00000000000..3be244dbda4
--- /dev/null
+++ b/spec/support/gitlab-git-test.git/objects/e3/7697aea12699f0b44544332a7c0f41ace5fb16
@@ -0,0 +1,2 @@
+x¥ŽK
+Â0EgNIÒ|ADtè*^’
mZ	qGîčY×àð8—×ZK©ý®7"ÈFc’Ò%oH¢D²Ü9rZÛLÎs“MJ2Œ™=±ÑÒAå…CmeFg²·V¨xI9øH2†¯þXÜJ…ár»pÅ6‡Ï;NÔà8•zˆ??>ß+–ù×z¡¹WƏBÞÎÙf·Ç}«þßb¡N@K\SYîì•iSC
\ No newline at end of file
diff --git a/spec/support/gitlab-git-test.git/objects/eb/a0c153ed20d927bab00507f356043b6b4be31e b/spec/support/gitlab-git-test.git/objects/eb/a0c153ed20d927bab00507f356043b6b4be31e
new file mode 100644
index 0000000000000000000000000000000000000000..2bf27fe50484b8d1a97340729fb4f05b1b53ef94
GIT binary patch
literal 185
zcmV;q07n0K0iBLPYQr!PMZ4B1hHL{tvS(xoN-4=Qrx;B}sc>W$q1-4p$;IkGPmtw*
z@E=dsT1$t4;zw_)AR#$U&P$*meo8)ARYs2}+0%f5G7(8Ozua``fC-}uOw*7#_#CIn
zDJ4dm(*=V%O?K2lyy1KQSsR>7h28Od+QEt2Ej=GHYlF|Z)PuYLzxMaFa=9I3tzY2C
nM)YxX1bgeOH5=WE`}TU*MKu1qOlrzA=pyT-ELZafWgk-vk#Jb0

literal 0
HcmV?d00001

diff --git a/spec/support/gitlab-git-test.git/objects/f6/5ad228d96e2a2ae7088e8557fe8906f6dd2b3f b/spec/support/gitlab-git-test.git/objects/f6/5ad228d96e2a2ae7088e8557fe8906f6dd2b3f
new file mode 100644
index 0000000000000000000000000000000000000000..8ab8606c6be3aa3305d6ab06b9b9846a4cecaf17
GIT binary patch
literal 642
zcmV-|0)72>0V^p=O;s>AFk>(@FfcPQQP8l^ypxn#q!5spnOBmOn3JPdoLs^1Z0@7k
zn=W2+_U_hPR~~ia(376viBJuC>6s;&>3R7@sSNj5*-t-ZT&E!FHDk?O?&rC8<<B%B
zsmjeyDa}bOX1H5(`}6)T{>&enBRJk^MC?_`ao2(>bM|obb9eRecV{^6Si1hAc!kWK
z8D=TQv-l%tM{o9nDf0IV3G#Fb4e|7I*UL>|I9SmZzB6*hqL1ZEe&l8><JW2stcNOf
zPt8rs%t_U&$}C{$+nW+;E|<R1EY?_~L%Cnh@zZ%7s4^c<XIH;qSBA~2E{AQ_@0%@J
zAlmbC8b`4x_tCRZCBBZHej%=Yj(*OrAUmht{IK--ZdSj!m#2qz?zn%f^ATSMRAoSr
zzq4y_Fi6$@(gM{ZJW0>~FZ(EKA$dG=dG0<=sHz}WM;BkPQNPwpc70x)yWeq|NLqo-
z!7DY17XqP*!d!!bJ^lR{X123$Q9LpG=3+Ku<DcI(-rG4}WHd1V0)?c^JcbosQ#qD4
zJMGh1a?&PM`N+?`@h|T}WK&X83sQ?17Bx+BWthNhA%0Eengx%1^#8{X{UHic^OEyZ
zGV{_I0-9d<`t6lIAQG}EI6pj+UEnUqM~D(o_!Tp3HV&?_eS7`Q#+UA!weO}_JHL;4
zWoQO=0x<q_5|eb(ixQKw8B*eNe475+$FY4YG;!q$=dO$kU52WzI3qPDhoN%Mt7mhz
zmhz^iZ&Hq|oVkCc%;udC`|?tYOHxx9DjulWt7RP&J3MWcdh7K-_S<g??V$!0XI3yc
c%r`&sAtLq3Z-t(6k=9Rwi*$T+0S~qm_BYx@LjV8(

literal 0
HcmV?d00001

-- 
2.30.9