1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use super::read_file;
use super::{
    config::RepositorySavedState, env::Environment, TargetRepository, TargetRepositoryError,
};

use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;

use log::{debug, error, info};
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::process::{Child, Command, ExitStatus, Stdio};

pub struct StdoutTargetRepository<'a> {
    stdoutlock: std::io::StdoutLock<'a>,
}

impl<'a> From<std::io::StdoutLock<'a>> for StdoutTargetRepository<'a> {
    fn from(value: std::io::StdoutLock<'a>) -> Self {
        Self { stdoutlock: value }
    }
}

impl<'a> TargetRepository for StdoutTargetRepository<'a> {
    fn start_import(
        &mut self,
        _git_active_branches: Option<usize>,
    ) -> Result<(&mut Write, Option<RepositorySavedState>), TargetRepositoryError> {
        Ok((&mut self.stdoutlock, None))
    }
    fn finish(&mut self) -> Result<(), TargetRepositoryError> {
        Ok(())
    }
}

pub struct GitTargetRepository<'a> {
    path: PathBuf,
    fast_import_cmd: Option<Child>,
    saved_state: Option<RepositorySavedState>,
    env: Option<&'a Environment>,
}

impl<'a> GitTargetRepository<'a> {
    pub fn open<P: AsRef<Path>>(value: P) -> Self {
        Self {
            path: value.as_ref().into(),
            fast_import_cmd: None,
            saved_state: None,
            env: None,
        }
    }

    pub fn set_env(&mut self, value: &'a Environment) {
        self.env = Some(value);
    }

    fn get_saved_state_path(&self) -> PathBuf {
        let mut saved_state = self.path.join(".git").join(env!("CARGO_PKG_NAME"));
        saved_state.set_extension("lock");
        saved_state
    }

    pub fn create_repo(&self) -> Result<(), TargetRepositoryError> {
        let path = &self.path;
        info!("Creating new dir");
        fs::create_dir_all(path)?;

        info!("Init Git repo");
        let status = Command::new("git").arg("init").current_dir(path).status()?;
        if !status.success() {
            error!("Cannot init Git repo");
            return Err(TargetRepositoryError::CannotInitRepo(status));
        }

        info!("Configure Git repo");
        let status = Command::new("git")
            .args(&["config", "core.ignoreCase", "false"])
            .current_dir(path)
            .status()?;
        if !status.success() {
            error!("Cannot configure Git repo");
            return Err(TargetRepositoryError::CannotConfigRepo(status));
        }

        info!("New Git repo initialization done");

        Ok(())
    }

    fn git(&self, args: &[&str], quiet: bool) -> ExitStatus {
        self.git_cmd_quiet(|cmd| cmd.args(args), quiet)
    }

    fn git_cmd_quiet<F>(&self, mut f: F, quiet: bool) -> ExitStatus
    where
        F: FnMut(&mut Command) -> &mut Command,
    {
        self.git_cmd(|mut cmd| {
            f(&mut cmd);
            if quiet {
                cmd.arg("--quiet");
            }
        })
    }

    fn git_cmd<F>(&self, mut f: F) -> ExitStatus
    where
        F: FnMut(&mut Command),
    {
        let mut git_cmd = Command::new("git");
        f(&mut git_cmd);
        git_cmd.current_dir(&self.path).status().unwrap()
    }
}

impl<'a> TargetRepository for GitTargetRepository<'a> {
    fn start_import(
        &mut self,
        git_active_branches: Option<usize>,
    ) -> Result<(&mut Write, Option<RepositorySavedState>), TargetRepositoryError> {
        let path = &self.path;
        let saved_state;
        info!("Checking Git repo: {}", path.to_str().unwrap());

        let clean = self.env.map(|x| x.clean).unwrap_or_default();
        if path.exists() && clean {
            info!("Path exists, removing because of clean option");
            std::fs::remove_dir_all(path)?;
        }

        if path.exists() {
            if path.is_dir() {
                info!("Path exists, checking for saved state");

                let saved_state_path = self.get_saved_state_path();

                if !saved_state_path.exists() {
                    return Err(TargetRepositoryError::SavedStateDoesNotExist);
                }

                let saved_state_str = read_file(&saved_state_path)?;
                let loaded_saved_state: RepositorySavedState =
                    toml::from_str(&saved_state_str).unwrap();

                info!("Loaded saved state: {:?}", loaded_saved_state);
                saved_state = Some(loaded_saved_state);
            } else {
                error!("Path must be directory");
                return Err(TargetRepositoryError::IsNotDir);
            }
        } else {
            self.create_repo()?;
            saved_state = None;
        }

        let mut git = Command::new("git");
        let mut git_cmd = git.args(&[
            "fast-import",
            "--export-marks=.git/hg-git-fast-import.marks",
            "--import-marks-if-exists=.git/hg-git-fast-import.marks",
            "--quiet",
        ]);
        if let Some(git_active_branches) = git_active_branches {
            git_cmd = git_cmd.arg(format!("--active-branches={}", git_active_branches));
        }
        self.fast_import_cmd = Some(git_cmd.current_dir(path).stdin(Stdio::piped()).spawn()?);

        Ok((
            self.fast_import_cmd
                .as_mut()
                .map(|x| x.stdin.as_mut().unwrap())
                .unwrap(),
            saved_state,
        ))
    }

    fn finish(&mut self) -> Result<(), TargetRepositoryError> {
        info!("Waiting for Git fast-import to finish");

        let status = self.fast_import_cmd.as_mut().unwrap().wait()?;
        info!("Finished");

        let cron = self.env.map(|x| x.cron).unwrap_or_default();

        let status = if status.success() {
            info!("Checking out HEAD revision");
            self.git(&["checkout", "HEAD"], cron)
        } else {
            error!("Git fast-import failed.");
            return Err(TargetRepositoryError::ImportFailed(status));
        };

        let status = if status.success() {
            info!("Resetting Git repo.");
            self.git(&["reset", "--hard"], cron)
        } else {
            panic!("Cannot checkout HEAD revision in Git repo.")
        };

        let status = if status.success() {
            info!("Cleanup Git repo");
            self.git(&["clean", "-d", "-x", "-f"], cron)
        } else {
            panic!("Cannot reset Git repo.")
        };
        if !status.success() {
            panic!("Cannot cleanup Git repo.");
        };

        let (target_push, target_pull) = self
            .env
            .map(|x| (x.target_push, x.target_pull))
            .unwrap_or_default();

        if target_pull {
            info!("Pulling Git repo.");
            self.fetch_all()?;
        }

        if target_push {
            info!("Pushing Git repo.");
            let status = self.git(&["push", "--all"], cron);
            let status = if status.success() {
                self.git(&["push", "--tags"], cron)
            } else {
                panic!("Cannot push all to Git repo.");
            };
            if !status.success() {
                panic!("Cannot push all tags to Git repo.");
            };
        }

        Ok(())
    }

    fn verify(
        &self,
        verified_repo: &str,
        subfolder: Option<&str>,
    ) -> Result<(), TargetRepositoryError> {
        info!("Verifying...");

        let path: String = subfolder.map_or_else(
            || self.path.to_str().unwrap().into(),
            |subfolder| self.path.join(subfolder).to_str().unwrap().into(),
        );

        info!(
            "Verify - Mercurial (source): {} vs Git (target): {}",
            verified_repo, path
        );
        let status = Command::new("diff")
            .args(&[
                "-ur",
                "--exclude=.hg",
                "--exclude=.idea",
                "--exclude=.git",
                "--exclude=*.iml",
                "--exclude=target",
                "--exclude=.hgtags",
                verified_repo,
                &path,
            ])
            .status()
            .unwrap();
        if status.success() {
            Ok(())
        } else {
            Err(TargetRepositoryError::VerifyFail)
        }
    }

    fn get_saved_state(&self) -> Option<&RepositorySavedState> {
        self.saved_state.as_ref()
    }

    fn save_state(&self, state: RepositorySavedState) -> Result<(), TargetRepositoryError> {
        let path = &self.path;
        info!("Saving state to Git repo: {}", path.to_str().unwrap());
        let saved_state_path = self.get_saved_state_path();
        let toml = toml::to_string(&state).unwrap();
        let mut f = File::create(&saved_state_path)?;
        f.write_all(toml.as_bytes())?;
        Ok(())
    }

    fn remote_list(&self) -> Result<HashSet<String>, TargetRepositoryError> {
        debug!("git remote");
        let output = Command::new("git")
            .arg("remote")
            .current_dir(&self.path)
            .output()?;
        Ok(output
            .stdout
            .split(|&x| x == b'\n')
            .filter_map(|x| {
                if !x.is_empty() {
                    Some(std::str::from_utf8(x).unwrap().into())
                } else {
                    None
                }
            })
            .collect())
    }

    fn remote_add(&self, name: &str, url: &str) -> Result<(), TargetRepositoryError> {
        debug!("git remote add {} {}", name, url);
        Command::new("git")
            .args(&["remote", "add", name, url])
            .current_dir(&self.path)
            .status()?;
        Ok(())
    }

    fn checkout(&self, branch: &str) -> Result<(), TargetRepositoryError> {
        debug!("git checkout -B {}", branch);
        Command::new("git")
            .args(&["checkout", "-B", branch])
            .current_dir(&self.path)
            .status()?;
        Ok(())
    }

    fn merge_unrelated(&self, branches: &[&str]) -> Result<(), TargetRepositoryError> {
        debug!(
            "git merge -n --allow-unrelated-histories --no-edit {}",
            branches.join(" ")
        );
        Command::new("git")
            .args(&["merge", "-n", "--allow-unrelated-histories", "--no-edit"])
            .args(branches)
            .current_dir(&self.path)
            .status()?;
        Ok(())
    }

    fn fetch_all(&self) -> Result<(), TargetRepositoryError> {
        debug!("git fetch -q --all");
        Command::new("git")
            .args(&["fetch", "-q", "--all"])
            .current_dir(&self.path)
            .status()?;

        debug!("git fetch -q --tags");
        Command::new("git")
            .args(&["fetch", "-q", "--tags"])
            .current_dir(&self.path)
            .status()?;
        Ok(())
    }
}