<commit_before>//@flow
const cp     = require('child_process')
const crypto = require('crypto')
const path   = require('path')
const fs     = require('fs')

const {store, actions} = require('./actions')

import type {RepoStatus} from './reducers'


let prev_state = store.getState()
store.subscribe(handleChanges)

function handleChanges() {
    const state = store.getState()
    if (! state.equals(prev_state)) {
        console.log(state)
        const previous_sessions = prev_state.get('sessions')
        state.get('sessions').forEach((session, id) =>  {
            if (! session.equals(previous_sessions.get(id))) {
                handleSessionChanges(session, id)
            }
        })
    }
    prev_state = state
}

function handleSessionChanges(session, id) {
    session.get('repos').forEach((repo, url) => {
        const status = repo.get('status')
        if (status === 'start') {
            return startClone(id, url)
        }
    })
}

function startClone(id, url) {
    const slug = hash(url)
    const folder = urlToFolder(id, slug)
    return fs.exists(folder, exists => {
        if (exists) {
            return pull(id, url, slug)
        } else {
            return clone(id, url, slug)
        }
    })
}

function pull(id, url, slug) {
    cp.exec(`cd ${urlToFolder(id, slug)} && git pull`)
        .on('exit', reportStatus.bind(null, id, url))
    return actions.reportCloneStatus(id, {url, status:'cloning', slug})
}

function clone(id, url, slug) {
    cp.exec(`git clone --depth=1 ${url} ${urlToFolder(id, slug)}`)
        .on('exit', reportStatus.bind(null, id, url))
    return actions.reportCloneStatus(id, {url, status:'cloning', slug})
}

function reportStatus(id, url, processStatus) {
    if (processStatus !== 0) {
        actions.reportCloneStatus(id, {url, status:'clone_failed'})
    } else {
        actions.reportCloneStatus(id, {url, status:'clone_done'})
    }
}

function urlToFolder(id, slug)  {
    return path.join('./tmp', id, slug)
}

function hash(str) {
    return crypto.createHash('sha1').update(str).digest('hex')
}
<commit_msg>Refactor folder stuff in handle_changes.js<commit_after>//@flow
const cp     = require('child_process')
const crypto = require('crypto')
const path   = require('path')
const fs     = require('fs')

const {store, actions} = require('./actions')

import type {RepoStatus} from './reducers'


let prev_state = store.getState()
store.subscribe(handleChanges)

function handleChanges() {
    const state = store.getState()
    if (! state.equals(prev_state)) {
        console.log(state)
        const previous_sessions = prev_state.get('sessions')
        state.get('sessions').forEach((session, id) =>  {
            if (! session.equals(previous_sessions.get(id))) {
                handleSessionChanges(session, id)
            }
        })
    }
    prev_state = state
}

function handleSessionChanges(session, id) {
    session.get('repos').forEach((repo, url) => {
        const status = repo.get('status')
        if (status === 'start') {
            return startClone(id, url)
        }
    })
}

function startClone(id, url) {
    const slug = hash(url)
    const folder = toFolder(id, slug)
    return fs.exists(folder, exists => {
        if (exists) {
            return pull(id, url, slug)
        } else {
            return clone(id, url, slug)
        }
    })
}

function pull(id, url, slug) {
    const folder = toFolder(id, slug)
    cp.exec(`cd ${folder} && git fetch && git reset --hard origin/HEAD`)
        .on('exit', reportStatus.bind(null, id, url))
    return actions.reportCloneStatus(id, {url, status:'cloning', slug})
}

function clone(id, url, slug) {
    const folder = toFolder(id, slug)
    cp.exec(`git clone --depth=1 ${url} ${folder}`)
        .on('exit', reportStatus.bind(null, id, url))
    return actions.reportCloneStatus(id, {url, status:'cloning', slug})
}

function reportStatus(id, url, processStatus) {
    if (processStatus !== 0) {
        actions.reportCloneStatus(id, {url, status:'clone_failed'})
    } else {
        actions.reportCloneStatus(id, {url, status:'clone_done'})
    }
}

function toFolder(id, slug)  {
    return path.join('./tmp', id, slug)
}

function hash(str) {
    return crypto.createHash('sha1').update(str).digest('hex')
}

{'fraction_non_alphanumeric': 0.15492300513299112, 'fraction_numerical': 0.0013999066728884741, 'mean_word_length': 2.9547970479704797, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4401634', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1341, 'n_words': 304}