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