<commit_before>'use strict';

angular.module('sv')
  .controller('MainCtrl', function ($scope, $timeout, GithubAPI) {
	
    $scope.user = {'username': '', 'repos':[], 'branches':[], 'currentRepo':'', 'currentBranch': '', 'currentCommits':[]}
    
	//var dbClient = new Dropbox.Client({ key: '<KEY>'});
	if(!$scope.dbClient){
		$scope.dbClient = new Dropbox.Client({ key: '<KEY>' });
	}
	
	if(!$scope.dbClient.isAuthenticated()){
		$scope.dbClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl:  'https://ronlaflamme.github.io/sv/oauth_receiver.html' }));
		$scope.dbClient.authenticate(function(authError){
		if(authError || !$scope.dbClient.isAuthenticated()){
			alert("Cannot login to Dropbox!");
		}});
	}
	
	$scope.usernameChange = function(){		
		$scope.user.branches = [];
		$scope.user.repos = [];
		$scope.user.currentRepo = '';
		$scope.user.currentBranch = '';
		$scope.user.currentCommits = [];
		
        GithubAPI.getRepos($scope.user.username).then(function(data){
            $scope.user.repos = [];
            angular.forEach(data, function(repo){
                $scope.user.repos.push(repo.full_name.split("/").pop());
            });
        });
    }
    
    $scope.repoChanged = function(){
        $scope.user.branches = [];
        GithubAPI.getBranches($scope.user.username, $scope.user.currentRepo).then(function(data){
            angular.forEach(data, function(branch){
                $scope.user.branches.push(branch.name);
            });
			$scope.user.currentBranch = 'master';
        });
    }
	$scope.$watch("user.currentBranch", function(newVal){
		if (newVal){
			branchChanged();
		}
	});

    var branchChanged = function(){
        GithubAPI.getCommits($scope.user.username, $scope.user.currentRepo, $scope.user.currentBranch).then(function(data){
            $scope.user.currentCommits = [];
			
			// set log filename to most recent commit date
			if(data && data.length > 0){
				$scope.dbFilename="SV_" + data[0].commit.committer.date + ".log";
			}
			
			if($scope.dbClient.isAuthenticated()){				
				angular.forEach(data, function(commit, key){
					GithubAPI.getCommit($scope.user.username, $scope.user.currentRepo,
										 commit.sha).then(function(commitInfo){
											 
					if(commitInfo.hasOwnProperty("files") &&
						commitInfo.files.length > 0){	
						
						// grab first file in commit and retrieve DB host_id
						var filename = $scope.user.currentRepo + '/' + commitInfo.files[0].filename;
						
						$scope.dbClient.history(filename, function(error, revisions){
							var hostID;
							if(error){
								hostID = error.responseText;
							}
							else{
								hostID = revisions[0]["host_id"];
							}
							
							$timeout(function(){
								$scope.user.currentCommits.push({
								'timestamp': commit.commit.committer.date, 
								'hostId':  hostID,
								'commit': commit.sha});
							});
							
						});
					}
					});
				});
			}	
		});
    }
		
	$scope.saveToDropboxClick = function(){
		
		if(!$scope.dbClient){
			$scope.dbClient = new Dropbox.Client({ key: '<KEY>' });
		}
			
		if(!$scope.dbClient.isAuthenticated()){<commit_msg>Adding host_id matching for each commit<commit_after>'use strict';

angular.module('sv')
  .controller('MainCtrl', function ($scope, $timeout, GithubAPI) {
	
    $scope.user = {'username': '', 'repos':[], 'branches':[], 'currentRepo':'', 'currentBranch': '', 'currentCommits':[]}
    
	//var dbClient = new Dropbox.Client({ key: 'ul8h8jpx9o164n1'});
	if(!$scope.dbClient){
		$scope.dbClient = new Dropbox.Client({ key: '<KEY>' });
	}
	
	if(!$scope.dbClient.isAuthenticated()){
		$scope.dbClient.authDriver(new Dropbox.AuthDriver.Popup({ receiverUrl:  'https://ronlaflamme.github.io/sv/oauth_receiver.html' }));
		$scope.dbClient.authenticate(function(authError){
		if(authError || !$scope.dbClient.isAuthenticated()){
			alert("Cannot login to Dropbox!");
		}});
	}
	
	$scope.usernameChange = function(){		
		$scope.user.branches = [];
		$scope.user.repos = [];
		$scope.user.currentRepo = '';
		$scope.user.currentBranch = '';
		$scope.user.currentCommits = [];
		
        GithubAPI.getRepos($scope.user.username).then(function(data){
            $scope.user.repos = [];
            angular.forEach(data, function(repo){
                $scope.user.repos.push(repo.full_name.split("/").pop());
            });
        });
    }
    
    $scope.repoChanged = function(){
        $scope.user.branches = [];
        GithubAPI.getBranches($scope.user.username, $scope.user.currentRepo).then(function(data){
            angular.forEach(data, function(branch){
                $scope.user.branches.push(branch.name);
            });
			$scope.user.currentBranch = 'master';
        });
    }
	$scope.$watch("user.currentBranch", function(newVal){
		if (newVal){
			branchChanged();
		}
	});

    var branchChanged = function(){
        GithubAPI.getCommits($scope.user.username, $scope.user.currentRepo, $scope.user.currentBranch).then(function(data){
            $scope.user.currentCommits = [];
			
			// set log filename to most recent commit date
			if(data && data.length > 0){
				$scope.dbFilename="SV_" + data[0].commit.committer.date + ".log";
			}
			
			if($scope.dbClient.isAuthenticated()){				
				for(var i = 0; i < data.length; i++){
					var commit = data[i];
					var commitDate = commit.commit.committer.date;
					var previousCommitDate = i + 1 < data.length ? 
										data[i+1].commit.committer.date : null;
					GithubAPI.getCommit($scope.user.username, $scope.user.currentRepo,
										 commit.sha).then(function(commitInfo){
											
											
					if(commitInfo.hasOwnProperty("files") &&
						commitInfo.files.length > 0){	
						
						// grab first file in commit and retrieve DB host_id
						var filename = $scope.user.currentRepo + '/' + commitInfo.files[0].filename;
						
						$scope.dbClient.history(filename, function(error, revisions){
							var hostID;
							if(error){
								hostID = error.responseText;
							}
							else if(revisions && revisions.length > 0){		
								hostID = "Not found";							
								for(var i = 0; i < revisions.length; i++){
									var revisionDate = revisions[i].modifiedAt;
									if(revisionDate <= commitDate &&
										revisionDate >= previousCommitDate){
											hostID = revisions[i]["host_id"];
											break;
										}										
								}								
							}
							else{
								hostId = "Unable to retrieve revision history for commit";
							}
							
							$timeout(function(){
								$scope.user.currentCommits.push({
								'timestamp': commitDate, 
								'hostId':  hostID,
								'commit': commit.sha});
							});
							
						});
					}
					});
				}
			}	
		});
    }
		
	$scope.saveToDropboxClick = function(){
		
		if(!$scope.dbClient){
			$scope.dbClient = new Dropbox.Client({ key: '<KEY>' });
		}
			
		if(!$scope.dbClient.isAuthenticated()){
{'fraction_non_alphanumeric': 0.16859623733719248, 'fraction_numerical': 0.0030390738060781476, 'mean_word_length': 2.6032325338894684, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 2, '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': '3928797', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2183, 'n_words': 325}