Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Import: the same file imported on two devices before they sync merges into one copy

Duarte Santos74f0c3cce887
diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js
index 086e9af9d..c4e739786 100644
--- a/frontend/src/lib/import-csv.js
+++ b/frontend/src/lib/import-csv.js
@@ -443,6 +443,22 @@ const toKm = (v, unit) => num(v) * (KM[String(unit || 'km').toLowerCase().trim()
* of several thousand sets will contain oddities, and losing the file over one of them
* helps nobody. Bad rows are counted and reported instead.
*/
+// Ids of what an import makes, from what it is rather than at random: the same file imported on
+// two devices before they synced made every session and custom exercise twice (two random ids
+// each), and the merge, which unites by id, kept both. The same session from the same app now has
+// the same id wherever it is imported, and the merge keeps one. (cyrb53, 53 bits, base 36.)
+export function importId(prefix, text) {
+ let h1 = 0xdeadbeef, h2 = 0x41c6ce57
+ for (let i = 0; i < text.length; i++) {
+ const ch = text.charCodeAt(i)
+ h1 = Math.imul(h1 ^ ch, 2654435761)
+ h2 = Math.imul(h2 ^ ch, 1597334677)
+ }
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
+ return prefix + (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36)
+}
+
export function parseWorkoutCSV(text, { unit = 'kg' } = {}) {
const rows = parseCSV(text)
if (rows.length < 2) return { error: 'empty' }
@@ -519,7 +535,7 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) {
let c = created.get(key)
if (!c) {
c = {
- id: 'im' + uid(), n: name.toLowerCase(), custom: true, eq: 'custom', tg: '', desc: '',
+ id: importId('im', source + '|' + name.toLowerCase()), n: name.toLowerCase(), custom: true, eq: 'custom', tg: '', desc: '',
bp: CATEGORY_BP[cell(r, 'category').toLowerCase()] || (km ? 'cardio' : null)
// Seconds with nothing else is a hold when the name says what it is (a plank is
// core work); a name that says nothing keeps being read as cardio, as before.
@@ -584,7 +600,7 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) {
}
const ss = cell(r, 'superset')
if (ss && !day.sg.has(id)) {
- if (!day.groups.has(ss)) day.groups.set(ss, 'is' + uid())
+ if (!day.groups.has(ss)) day.groups.set(ss, importId('is', when.d + '|' + ss))
day.sg.set(id, day.groups.get(ss))
}
sets++
@@ -624,7 +640,7 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) {
const start = base + (day.start ?? 18 * 3600000)
const end = day.end != null ? base + day.end : start
const w = {
- id: 'iw' + uid(), d, start, end: end > start ? end : start,
+ id: importId('iw', JSON.stringify([source, d, start, day.name || '', entries.map(e => [e.id, e.sets])])), d, start, end: end > start ? end : start,
routineId: null, name: day.name || 'Imported', entries, prs: [],
...(day.note ? { note: day.note } : {}),
}
@@ -749,7 +765,9 @@ export function mergeImport(S, parsed) {
const fresh = parsed.workouts.filter(w => !have.has(w.d))
.map(w => ({ ...w, entries: w.entries.map(e => (exIdMap[e.id] ? { ...e, id: exIdMap[e.id] } : e)) }))
const used = new Set(fresh.flatMap(w => w.entries.map(e => e.id)))
- const customs = parsed.customEx.filter(c => used.has(c.id) && !EXIDX[c.id])
+ // (one an earlier import of the same name already made has the same id now: kept once)
+ const known = new Set(S.customEx.map(c => c?.id))
+ const customs = parsed.customEx.filter(c => used.has(c.id) && !EXIDX[c.id] && !known.has(c.id))
S.customEx = [...S.customEx, ...customs]
S.workouts = [...S.workouts, ...fresh].sort((a, b) => (a.d < b.d ? -1 : 1))
// seed the weight suggestions from the newest imported set of each lift
diff --git a/frontend/src/lib/import-csv.twice.test.js b/frontend/src/lib/import-csv.twice.test.js
new file mode 100644
index 000000000..9790daf9b
--- /dev/null
+++ b/frontend/src/lib/import-csv.twice.test.js
@@ -0,0 +1,30 @@
+/* The same CSV imported on two devices before they synced made every session (and every custom
+ exercise) twice: random ids each time, and the merge unites by id. Import ids now come from what
+ is imported. (QA 2026-10-06, also in v1.3.9.) */
+import { describe, expect, it } from 'vitest'
+import { parseWorkoutCSV, mergeImport } from './import-csv.js'
+import { mergeStates } from './sync-merge.js'
+
+const CSV = [
+ 'Date,Workout Name,Duration,Exercise Name,Set Order,Weight,Reps,Distance,Seconds,Notes,Workout Notes,RPE',
+ '2026-09-01 18:00:00,Push,1h,Bench Press (Barbell),1,80,5,0,0,,,',
+ '2026-09-01 18:00:00,Push,1h,Grip Trainer Deluxe,1,30,10,0,0,,,',
+ '2026-09-03 18:00:00,Pull,1h,Deadlift (Barbell),1,140,5,0,0,,,',
+].join('\n')
+const base = () => ({ _ts: 1, workouts: [], customEx: [], bodyweight: [], exWeights: {}, routines: [] })
+
+describe('the same import on two devices', () => {
+ it('merges into one copy of each session and custom exercise', () => {
+ const A = base(), B = base()
+ mergeImport(A, parseWorkoutCSV(CSV, { unit: 'kg' })); A._ts = 10
+ mergeImport(B, parseWorkoutCSV(CSV, { unit: 'kg' })); B._ts = 20
+ expect(A.workouts).toHaveLength(2)
+ const M = mergeStates(A, B)
+ expect(M.workouts).toHaveLength(2)
+ expect(M.customEx).toHaveLength(A.customEx.length)
+ })
+ it('different sessions still get different ids', () => {
+ const p = parseWorkoutCSV(CSV, { unit: 'kg' })
+ expect(new Set(p.workouts.map(w => w.id)).size).toBe(2)
+ })
+})
diff --git a/frontend/src/lib/import-hevy.js b/frontend/src/lib/import-hevy.js
index eb0879e98..851aa74c0 100644
--- a/frontend/src/lib/import-hevy.js
+++ b/frontend/src/lib/import-hevy.js
@@ -9,7 +9,7 @@ import { EXIDX } from './exercises.js'
import { uid } from './format.js'
import { isWarmupRow } from './workout-model.js'
import { HEVY_ID_MAP, HEVY_TITLE_MAP } from './hevy-id-map.js'
-import { daysBetween } from './import-csv.js'
+import { daysBetween, importId } from './import-csv.js'
export { HEVY_ID_MAP, HEVY_TITLE_MAP }
export const HEVY_API = 'https://api.hevyapp.com'
@@ -178,7 +178,7 @@ function makeResolver(templates) {
const t = templateId ? byId.get(templateId) : null
const name = (t?.title || fallbackTitle || 'exercise').toLowerCase()
c = {
- id: 'im' + uid(), n: name, custom: true, eq: 'custom', tg: '', desc: '',
+ id: importId('im', 'Hevy|' + key), n: name, custom: true, eq: 'custom', tg: '', desc: '',
bp: bpOfTemplate(t) || (t?.type === 'distance_duration' || t?.type === 'duration' ? 'cardio' : null)
|| 'upper legs',
}
@@ -284,7 +284,8 @@ export function parseHevyWorkouts(workouts, templates, { unit = 'kg' } = {}) {
const startMs = base + (day.start ?? 18 * 3600000)
const endMs = day.end != null ? base + day.end : startMs
const workout = {
- id: 'iw' + uid(), d, start: startMs, end: endMs > startMs ? endMs : startMs,
+ // From what it is (importId), so the same import on two devices merges into one.
+ id: importId('iw', JSON.stringify(['Hevy', d, startMs, day.name || '', entries.map(e => [e.id, e.sets])])), d, start: startMs, end: endMs > startMs ? endMs : startMs,
routineId: null, name: day.name || 'Imported', entries, prs: [],
}
// Work sets only, the number `workoutVolume` gives a workout finished in the app; it is stored for good.