Skip to content

Commit f4d8582

Browse files
committed
LFS: Improved verify and headers passing
1 parent 2ef2d5b commit f4d8582

File tree

5 files changed

+39
-46
lines changed

5 files changed

+39
-46
lines changed

src/main/kotlin/com/jetpackduba/gitnuro/lfs/GLfsFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class GLfsPrePushHook(
298298
// val credentials = credentialsStateManager.requestLfsCredentials()
299299

300300
lfsObjects.value.objects.forEach { obj ->
301-
val uploadUrl = obj.actions.upload?.href
301+
val uploadUrl = obj.actions?.upload?.href
302302

303303
if (uploadUrl != null) {
304304
lfsRepository.uploadObject(

src/main/kotlin/com/jetpackduba/gitnuro/lfs/LfsNetworkDataSource.kt

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.jetpackduba.gitnuro.lfs
22

33
import com.jetpackduba.gitnuro.Result
44
import com.jetpackduba.gitnuro.models.lfs.LfsObject
5+
import com.jetpackduba.gitnuro.models.lfs.LfsObjectBatch
56
import com.jetpackduba.gitnuro.models.lfs.LfsObjects
67
import com.jetpackduba.gitnuro.models.lfs.LfsPrepareUploadObjectBatch
78
import io.ktor.client.*
@@ -68,7 +69,7 @@ class LfsNetworkDataSource @Inject constructor(
6869
remoteUrl: String,
6970
lfsPrepareUploadObjectBatch: LfsPrepareUploadObjectBatch,
7071
username: String?,
71-
password: String?
72+
password: String?,
7273
): Result<LfsObjects, LfsError> {
7374
val response = client.post("${remoteUrl.removeSuffix("/")}/objects/batch") {
7475
this.headers {
@@ -125,29 +126,20 @@ class LfsNetworkDataSource @Inject constructor(
125126
password: String?,
126127
) {
127128
val response = client.put(uploadUrl) {
128-
if (username != null && password != null) {
129-
val objHeaders = lfsObject.actions.upload?.header
130-
131-
if (objHeaders.isNullOrEmpty()) {
132-
basicAuth(username, password)
133-
} else {
134-
for (header in objHeaders.entries) {
135-
header(header.key, header.value)
136-
}
137-
}
138-
129+
val objHeaders = lfsObject.actions?.upload?.header.orEmpty()
139130

140-
// if (authorization != null) {
141-
//
142-
// bearerAuth(authorization)
143-
// } else {
144-
// basicAuth(username, password)
145-
// }
131+
for (header in objHeaders.entries) {
132+
header(header.key, header.value)
146133
}
147134

148135
this.headers {
149-
this["Accept"] = "application/vnd.git-lfs"
150-
this["Content-Length"] = size.toString()
136+
if (username != null && password != null && !headers.contains("Authorization")) {
137+
basicAuth(username, password)
138+
}
139+
140+
if (!this.contains("Accept")) {
141+
this["Accept"] = "application/vnd.git-lfs"
142+
}
151143
}
152144

153145
setBody(file.readChannel())
@@ -159,26 +151,33 @@ class LfsNetworkDataSource @Inject constructor(
159151
}
160152

161153
override suspend fun verify(lfsObject: LfsObject, username: String?, password: String?) {
162-
val response = client.post(lfsObject.actions.verify!!.href) {
163-
if (username != null && password != null) {
164-
val objHeaders = lfsObject.actions.verify?.header
154+
val verify = lfsObject.actions?.verify
165155

166-
if (objHeaders.isNullOrEmpty()) {
167-
basicAuth(username, password)
168-
} else {
169-
for (header in objHeaders.entries) {
170-
header(header.key, header.value)
156+
if (verify != null) {
157+
val response = client.post(verify.href) {
158+
val objHeaders = verify.header.orEmpty()
159+
160+
for (header in objHeaders.entries) {
161+
header(header.key, header.value)
162+
}
163+
164+
this.headers {
165+
if (username != null && password != null && !headers.contains("Authorization")) {
166+
basicAuth(username, password)
167+
}
168+
169+
if (!this.contains("Accept")) {
170+
this["Accept"] = "application/vnd.git-lfs"
171171
}
172172
}
173-
}
174173

175-
this.headers {
176-
this["Accept"] = "application/vnd.git-lfs"
174+
val body = LfsObjectBatch(lfsObject.oid, lfsObject.size)
175+
setBody(json.encodeToString(body))
177176
}
178-
}
179177

180-
if (response.status != HttpStatusCode.OK) {
181-
throw Exception("Verify status is ${response.status} instead of HTTP OK 200")
178+
if (response.status != HttpStatusCode.OK) {
179+
throw Exception("Verify status is ${response.status} instead of HTTP OK 200")
180+
}
182181
}
183182
}
184183

src/main/kotlin/com/jetpackduba/gitnuro/lfs/LfsRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ fun createLfsPrepareUploadObjectBatch(
7373
transfers = listOf(
7474
"basic",
7575
// TODO Add support for standalone files and SSH once they are stable https://github.com/git-lfs/git-lfs/blob/main/docs/api/README.md
76-
//"lfs-standalone-file",
77-
//"ssh",
76+
"lfs-standalone-file",
77+
"ssh",
7878
),
7979
ref = LfsRef(branch),
8080
hashAlgo = algo,

src/main/kotlin/com/jetpackduba/gitnuro/lfs/LfsSmudgeFilter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class LfsSmudgeFilter(
139139
for (lfsObject in lfsObjects.objects) {
140140
var requiresAuth: Boolean
141141

142-
val downloadUrl = lfsObject.actions.download?.href ?: continue
142+
val downloadUrl = lfsObject.actions?.download?.href ?: continue
143143

144144
do {
145145
val newLfsObjects = lfsRepository.downloadObject(

src/main/kotlin/com/jetpackduba/gitnuro/models/lfs/LfsObjects.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ data class LfsObjects(
1212
@Serializable
1313
data class LfsObject(
1414
@SerialName("oid") var oid: String,
15-
@SerialName("size") var size: Int,
16-
@SerialName("actions") var actions: Actions,
15+
@SerialName("size") var size: Long,
16+
@SerialName("actions") var actions: Actions? = null,
1717
)
1818

1919
@Serializable
@@ -29,9 +29,3 @@ data class RemoteObjectAccessInfo(
2929
@SerialName("header") var header: Map<String, String>? = null,
3030
@SerialName("expires_at") var expiresAt: String? = null,
3131
)
32-
33-
//@Serializable
34-
//data class Header(
35-
// @SerialName("Accept") var accept: String? = null,
36-
// @SerialName("Authorization") var authorization: String? = null,
37-
//)

0 commit comments

Comments
 (0)