def ensure_commits(user, repo, sha: nil, return_retrieved: false,
                       num_commits: -1, fork_all: false)

      currepo = ensure_repo(user, repo)
      unless currepo[:forked_from].nil? or fork_all
        r = retrieve_repo(user, repo)
        return if r.nil?
        parent_owner = r['parent']['owner']['login']
        parent_repo  = r['parent']['name']
        ensure_fork_commits(user, repo, parent_owner, parent_repo)
        return
      end

      num_retrieved = 0
      commits = ['foo'] # Dummy entry for simplifying the loop below
      commit_acc = []
      until commits.empty?
        commits = retrieve_commits(repo, sha, user, 1)

        # This means that we retrieved the last commit page again
        if commits.size == 1 and commits[0]['sha'] == sha
          commits = []
        end

        retrieved = commits.map do |c|
          sha = c['sha']
          save{ensure_commit(repo, c['sha'], user)}
        end

        # Store retrieved commits to return, if client requested so
        if return_retrieved
          commit_acc = commit_acc << retrieved
        end

        num_retrieved += retrieved.size
        if num_commits > 0 and num_retrieved >= num_commits
          break
        end

      end

      commit_acc.flatten.select{|x| !x.nil?}
    end
def ensure_commits(user, repo, sha: nil, return_retrieved: false,
                       num_commits: -1, fork_all: false)

      currepo = ensure_repo(user, repo)
      unless currepo[:forked_from].nil? or fork_all
        r = retrieve_repo(user, repo)
        return if r.nil?
        parent_owner = r['parent']['owner']['login']
        parent_repo  = r['parent']['name']
        ensure_fork_commits(user, repo, parent_owner, parent_repo)
        return
      end

      num_retrieved = 0
      commits = ['foo'] # Dummy entry for simplifying the loop below
      commit_acc = []
      until commits.empty?
        commits = retrieve_commits(repo, sha, user, 1)

        # This means that we retrieved the last commit page again
        if commits.size == 1 and commits[0]['sha'] == sha
          commits = []
        end

        retrieved = commits.map do |c|
          sha = c['sha']
          save{ensure_commit(repo, c['sha'], user)}
        end

        # Store retrieved commits to return, if client requested so
        if return_retrieved
          commit_acc = commit_acc << retrieved
        end

        num_retrieved += retrieved.size
        if num_commits > 0 and num_retrieved >= num_commits
          break
        end

      end

      commit_acc.flatten.select{|x| !x.nil?}
    end
Retrieve commits for a repository, starting from +sha+
 ==Parameters:
 [user]  The user to whom the repo belongs.
 [repo]  The repo to look for commits into.
 [sha]   The first commit to start retrieving from. If nil, then retrieval
         starts from what the project considers as master branch.
 [return_retrieved] Should retrieved commits be returned? If not, memory is
                    saved while processing them.
 [num_commits] Number of commit to retrieve
 [fork_all] Retrieve all commits even if a repo is a fork