Class: GitPushService

Inherits:
BaseService show all
Includes:
Gitlab::Access, Gitlab::CurrentSettings
Defined in:
app/services/git_push_service.rb

Constant Summary

Constants included from Gitlab::Access

Gitlab::Access::DEVELOPER, Gitlab::Access::GUEST, Gitlab::Access::MASTER, Gitlab::Access::OWNER, Gitlab::Access::PROTECTION_DEV_CAN_PUSH, Gitlab::Access::PROTECTION_FULL, Gitlab::Access::PROTECTION_NONE, Gitlab::Access::REPORTER

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::Access

all_values, #human_access, options, options_with_owner, #owner?, protection_options, protection_values, sym_options, values

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Methods inherited from BaseService

#abilities, #can?, #deny_visibility_level, #event_service, #initialize, #log_info, #notification_service, #repository, #system_hook_service, #todo_service

Constructor Details

This class inherits a constructor from BaseService

Instance Attribute Details

#push_commitsObject

Returns the value of attribute push_commits


2
3
4
# File 'app/services/git_push_service.rb', line 2

def push_commits
  @push_commits
end

#push_dataObject

Returns the value of attribute push_data


2
3
4
# File 'app/services/git_push_service.rb', line 2

def push_data
  @push_data
end

Instance Method Details

#executeObject

This method will be called after each git update and only if the provided user and project are present in GitLab.

All callbacks for post receive action should be placed here.

Next, this method:

1. Creates the push event
2. Updates merge requests
3. Recognizes cross-references from commit messages
4. Executes the project's webhooks
5. Executes the project's services
6. Checks if the project's main language has changed

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/git_push_service.rb', line 19

def execute
  @project.repository.after_create if @project.empty_repo?
  @project.repository.after_push_commit(branch_name, params[:newrev])

  if push_remove_branch?
    @project.repository.after_remove_branch
    @push_commits = []
  elsif push_to_new_branch?
    @project.repository.after_create_branch

    # Re-find the pushed commits.
    if is_default_branch?
      # Initial push to the default branch. Take the full history of that branch as "newly pushed".
      process_default_branch
    else
      # Use the pushed commits that aren't reachable by the default branch
      # as a heuristic. This may include more commits than are actually pushed, but
      # that shouldn't matter because we check for existing cross-references later.
      @push_commits = @project.repository.commits_between(@project.default_branch, params[:newrev])

      # don't process commits for the initial push to the default branch
      process_commit_messages
    end
  elsif push_to_existing_branch?
    # Collect data for this git push
    @push_commits = @project.repository.commits_between(params[:oldrev], params[:newrev])
    process_commit_messages

    # Update the bare repositories info/attributes file using the contents of the default branches
    # .gitattributes file
    update_gitattributes if is_default_branch?
  end

  # Update merge requests that may be affected by this push. A new branch
  # could cause the last commit of a merge request to change.
  update_merge_requests

  # Checks if the main language has changed in the project and if so
  # it updates it accordingly
  update_main_language

  perform_housekeeping
end

#update_gitattributesObject


63
64
65
# File 'app/services/git_push_service.rb', line 63

def update_gitattributes
  @project.repository.copy_gitattributes(params[:ref])
end

#update_main_languageObject


67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/services/git_push_service.rb', line 67

def update_main_language
  # Performance can be bad so for now only check main_language once
  # See https://gitlab.com/gitlab-org/gitlab-ce/issues/14937
  return if @project.main_language.present?

  return unless is_default_branch?
  return unless push_to_new_branch? || push_to_existing_branch?

  current_language = @project.repository.main_language
  @project.update_attributes(main_language: current_language)
  true
end