Был произведен рефакторинг вызовов git.
Необходимо доработать create, так как при создании коммита в одной ветви проиходит "проброс" ненужных файлов в вышестоящие коммиты, следующих истории коммитов.
This commit is contained in:
		
							parent
							
								
									fe55e8680f
								
							
						
					
					
						commit
						dd5d57c75b
					
				
					 1 changed files with 87 additions and 174 deletions
				
			
		| 
						 | 
				
			
			@ -28,32 +28,28 @@ class Snag {
 | 
			
		|||
		_config = config;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private auto git(string[] command, string message, string separator = ":\n") {
 | 
			
		||||
		auto result = execute(_baseCommand ~ command);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				message ~ separator ~ result.output
 | 
			
		||||
			);
 | 
			
		||||
		return result;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void initialize() {
 | 
			
		||||
		auto result = execute(_baseCommand ~ "init");
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"A Git repository initialization error occurred:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		git(
 | 
			
		||||
			["init"],
 | 
			
		||||
			"A Git repository initialization error occurred"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["config", "user.email", _config.email]
 | 
			
		||||
		git(
 | 
			
		||||
			["config", "user.email", _config.email],
 | 
			
		||||
			"A Git repository initialization error occurred"
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"A Git repository initialization error occurred:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		git(
 | 
			
		||||
			["config", "user.name", _config.user],
 | 
			
		||||
			"A Git repository initialization error occurred"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["config", "user.name", _config.user]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"A Git repository initialization error occurred:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
		writeln(
 | 
			
		||||
			"The Git repository has been initialized successfully: "
 | 
			
		||||
			~ _config.git
 | 
			
		||||
| 
						 | 
				
			
			@ -61,50 +57,36 @@ class Snag {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	void status() {
 | 
			
		||||
		auto result = execute(
 | 
			
		||||
			_baseCommand ~ ["status", "--porcelain"]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"An error occurred while checking the file tracking status:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		auto result = git(
 | 
			
		||||
			["status", "--porcelain"],
 | 
			
		||||
			"An error occurred while checking the file tracking status"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		writeln("The following list of files requires backup:");
 | 
			
		||||
 | 
			
		||||
		/**
 | 
			
		||||
		* Цепочка выполняет разбивку по переводу на новую строку,
 | 
			
		||||
		* отсеивает пустые строки, перебирает в цикле имеющиеся строки,
 | 
			
		||||
		* выводит только вторую часть строки (разделенную по пробелу)
 | 
			
		||||
		*/
 | 
			
		||||
		result.output.split('\n').filter!(e => !e.strip.empty).each!((e) {
 | 
			
		||||
			writefln("\t/%s", e.strip.split[1]);
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void create() {
 | 
			
		||||
		/**
 | 
			
		||||
		* Проверка файлов на состояние изменения
 | 
			
		||||
		*/
 | 
			
		||||
		auto result = execute(
 | 
			
		||||
			_baseCommand ~ ["status", "--porcelain"]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"An error occurred while checking the file tracking status:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		auto result = git(
 | 
			
		||||
			["status", "--porcelain"],
 | 
			
		||||
			"An error occurred while checking the file tracking status"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		if (!result.output.length)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Current file state doesn't need to be archived again"
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
				"rev-parse", "--short", "HEAD"
 | 
			
		||||
			]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
		result = execute(_baseCommand ~ ["rev-parse", "--short", "HEAD"]);
 | 
			
		||||
		if (result.status == 128) {
 | 
			
		||||
			git(["add", "."], "Failed to prepare files for archiving");
 | 
			
		||||
			git(["commit", "-m", "test"], "Failed to create a backup");
 | 
			
		||||
			writeln("Backup was created successfully");
 | 
			
		||||
			return;
 | 
			
		||||
		} else if (result.status != 0)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to retrieve current snapshot information:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
| 
						 | 
				
			
			@ -112,113 +94,67 @@ class Snag {
 | 
			
		|||
 | 
			
		||||
		string currentSnapshot = result.output.strip('\n');
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
				"checkout",
 | 
			
		||||
				"-b",
 | 
			
		||||
				"mod",
 | 
			
		||||
				currentSnapshot
 | 
			
		||||
			]
 | 
			
		||||
		git(
 | 
			
		||||
			["checkout", "-b", "mod", currentSnapshot ],
 | 
			
		||||
			"Failed to create a branch from the current state"
 | 
			
		||||
		);
 | 
			
		||||
		git(
 | 
			
		||||
			["add", "."],
 | 
			
		||||
			"Failed to prepare files for archiving"
 | 
			
		||||
		);
 | 
			
		||||
		git(
 | 
			
		||||
			["commit", "-m", "test"],
 | 
			
		||||
			"Failed to create a backup"
 | 
			
		||||
		);
 | 
			
		||||
		git(
 | 
			
		||||
			["checkout", "master"],
 | 
			
		||||
			"Failed to perform intermediate state switching"
 | 
			
		||||
		);
 | 
			
		||||
		git(
 | 
			
		||||
			["rebase", "--onto", "mod", currentSnapshot, "master", "-X", "theirs"],
 | 
			
		||||
			"Ошибка при rebase"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["add", "."]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to prepare files for archiving:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		result = git(
 | 
			
		||||
			["rev-parse", "--short", "mod"],
 | 
			
		||||
			"An error occurred while checking the file tracking status"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["commit", "-m", "test"]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to create a backup:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
			);
 | 
			
		||||
		string newSnapshot = result.output.strip('\n');
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["checkout", "master"]
 | 
			
		||||
		git(
 | 
			
		||||
			["checkout", newSnapshot],
 | 
			
		||||
			"Failed to perform intermediate state switching"
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to perform intermediate state switching:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		git(
 | 
			
		||||
			["branch", "-D", "mod"],
 | 
			
		||||
			"Error while deleting temporary intermediate snapshot"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
				"merge",
 | 
			
		||||
				"-X",
 | 
			
		||||
				"theirs",
 | 
			
		||||
				"--squash",
 | 
			
		||||
				"mod"
 | 
			
		||||
			]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to retrieve changes from the new intermediate snapshot:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["commit", "-m", "test"]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to create backup after acquiring new intermediate snapshot:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
			);
 | 
			
		||||
		
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ ["branch", "-D", "mod"]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Error while deleting temporary intermediate snapshot:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
		writeln("Backup was created successfully");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void list() {
 | 
			
		||||
		auto result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
				"rev-parse", "--short", "HEAD"
 | 
			
		||||
			]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to retrieve current snapshot information:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		auto result = git(
 | 
			
		||||
			["rev-parse", "--short", "HEAD"],
 | 
			
		||||
			"Failed to retrieve current snapshot information"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		string current = result.output.strip('\n');
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
		result = git(
 | 
			
		||||
			[
 | 
			
		||||
				"log",
 | 
			
		||||
				"--all",
 | 
			
		||||
				"--date=format:%Y.%m.%d %H:%M",
 | 
			
		||||
				"--pretty=format:%ad\t%h"
 | 
			
		||||
			]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to retrieve the list of snapshots:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
			],
 | 
			
		||||
			"Failed to retrieve the list of snapshots"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		result.output.split('\n').each!((e) {
 | 
			
		||||
			auto eArray = e.split('\t');
 | 
			
		||||
			if (current == eArray[1])
 | 
			
		||||
				writefln("    >\t%s\t%s", eArray[0], eArray[1]);
 | 
			
		||||
			else
 | 
			
		||||
				writefln("\t%s\t%s", eArray[0], eArray[1]);
 | 
			
		||||
		});
 | 
			
		||||
		result.output.split('\n').map!(line => line.split('\t')).array
 | 
			
		||||
			.sort!((a, b) => a[0] > b[0]).each!(e =>
 | 
			
		||||
			writefln("%s\t%s\t%s", current == e[1] ? "    >" : "", e[0], e[1])
 | 
			
		||||
		);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void restore(string hash) {
 | 
			
		||||
| 
						 | 
				
			
			@ -227,48 +163,25 @@ class Snag {
 | 
			
		|||
				"Invalid snapshot hash provided"
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
		auto result = execute(
 | 
			
		||||
			_baseCommand ~ ["status", "--porcelain"]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"An error occurred while checking the file tracking status:\n"
 | 
			
		||||
				~ result.output
 | 
			
		||||
		auto result = git(
 | 
			
		||||
			["status", "--porcelain"],
 | 
			
		||||
			"An error occurred while checking the file tracking status"
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		if (result.output.length) {
 | 
			
		||||
			result = execute(
 | 
			
		||||
				_baseCommand ~ ["restore", "."]
 | 
			
		||||
			);
 | 
			
		||||
			if (result.status)
 | 
			
		||||
				throw new SnagException(
 | 
			
		||||
					"Failed to reset file changes state:\n"
 | 
			
		||||
					~ result.output
 | 
			
		||||
				);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
				"rev-parse", hash
 | 
			
		||||
			]
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"This snapshot is not available in the archive:\n"
 | 
			
		||||
				~ result.output.split('\n')[0]
 | 
			
		||||
		if (result.output.length)
 | 
			
		||||
			git(
 | 
			
		||||
				["restore", "."],
 | 
			
		||||
				"Failed to reset file changes state"
 | 
			
		||||
			);
 | 
			
		||||
 | 
			
		||||
		result = execute(
 | 
			
		||||
			_baseCommand ~ [
 | 
			
		||||
				"checkout", hash
 | 
			
		||||
			]
 | 
			
		||||
		git(
 | 
			
		||||
			["rev-parse", hash],
 | 
			
		||||
			"This snapshot is not available in the archive"
 | 
			
		||||
		);
 | 
			
		||||
		if (result.status)
 | 
			
		||||
			throw new SnagException(
 | 
			
		||||
				"Failed to restore the snapshot state %s:\n"
 | 
			
		||||
				.format(hash, result.output)
 | 
			
		||||
		git(
 | 
			
		||||
			["checkout", hash],
 | 
			
		||||
			"Failed to restore the snapshot state " ~ hash
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		writeln("Backup was restored successfully");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue