static int DoSyncWork(SyncOptions clo) {
string localGitDir = clo.LocalFolder != null && Path.IsPathRooted(clo.LocalFolder) ? clo.LocalFolder : Path.Combine(Environment.CurrentDirectory, clo.LocalFolder ?? repoPath);
EnsureGitDir(localGitDir);
string gitRepoPath = clo.Repo;
string username = clo.Login;
string password = clo.Password;
string gitlabauthtoken = clo.AuthToken;
string branchName = clo.Branch;
string trackerPath = clo.Tracker;
string gitServer = clo.Server;
DXVcsWrapper vcsWrapper = new DXVcsWrapper(vcsServer, username, password);
TrackBranch branch = FindBranch(branchName, trackerPath, vcsWrapper);
if (branch == null)
return 1;
string historyPath = GetVcsSyncHistory(vcsWrapper, branch.HistoryPath);
if (historyPath == null)
return 1;
SyncHistory history = SyncHistory.Deserialize(historyPath);
if (history == null)
return 1;
SyncHistoryWrapper syncHistory = new SyncHistoryWrapper(history, vcsWrapper, branch.HistoryPath, historyPath);
var head = syncHistory.GetHistoryHead();
if (head == null)
return 1;
GitLabWrapper gitLabWrapper = new GitLabWrapper(gitServer, gitlabauthtoken);
RegisteredUsers registeredUsers = new RegisteredUsers(gitLabWrapper, vcsWrapper);
User defaultUser = registeredUsers.GetUser(username);
if (!defaultUser.IsRegistered) {
Log.Error($"default user {username} is not registered in the active directory.");
return 1;
}
var checkMergeChangesResult = CheckChangesForMerging(gitLabWrapper, gitRepoPath, branchName, head, vcsWrapper, branch, syncHistory, defaultUser);
if (checkMergeChangesResult == CheckMergeChangesResult.NoChanges)
return 0;
if (checkMergeChangesResult == CheckMergeChangesResult.Error)
return 1;
GitWrapper gitWrapper = CreateGitWrapper(gitRepoPath, localGitDir, branch.Name, username, password);
if (gitWrapper == null)
return 1;
ProcessHistoryResult processHistoryResult = ProcessHistory(vcsWrapper, gitWrapper, registeredUsers, defaultUser, gitRepoPath, localGitDir, branch, clo.CommitsCount, syncHistory, true);
if (processHistoryResult == ProcessHistoryResult.NotEnough)
return 0;
if (processHistoryResult == ProcessHistoryResult.Failed)
return 1;
int result = ProcessMergeRequests(vcsWrapper, gitWrapper, gitLabWrapper, registeredUsers, defaultUser, gitRepoPath, localGitDir, clo.Branch, clo.Tracker, syncHistory, username);
if (result != 0)
return result;
return 0;
}
static CheckMergeChangesResult CheckChangesForMerging(GitLabWrapper gitLabWrapper, string gitRepoPath, string branchName, SyncHistoryItem head, DXVcsWrapper vcsWrapper, TrackBranch branch, SyncHistoryWrapper syncHistory, User defaultUser) {