xref: /OpenGrok/tools/src/main/python/opengrok_tools/scm/git.py (revision 2d57dc692b4dd10e696ca6922510f6cecded1bfd)
1*2d57dc69SVladimir Kotal#
2*2d57dc69SVladimir Kotal# CDDL HEADER START
3*2d57dc69SVladimir Kotal#
4*2d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the
5*2d57dc69SVladimir Kotal# Common Development and Distribution License (the "License").
6*2d57dc69SVladimir Kotal# You may not use this file except in compliance with the License.
7*2d57dc69SVladimir Kotal#
8*2d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific
9*2d57dc69SVladimir Kotal# language governing permissions and limitations under the License.
10*2d57dc69SVladimir Kotal#
11*2d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each
12*2d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt.
13*2d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the
14*2d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying
15*2d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner]
16*2d57dc69SVladimir Kotal#
17*2d57dc69SVladimir Kotal# CDDL HEADER END
18*2d57dc69SVladimir Kotal#
19*2d57dc69SVladimir Kotal
20*2d57dc69SVladimir Kotal#
21*2d57dc69SVladimir Kotal# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22*2d57dc69SVladimir Kotal# Portions Copyright (c) 2020, Krystof Tulinger <k.tulinger@seznam.cz>
23*2d57dc69SVladimir Kotal#
24*2d57dc69SVladimir Kotal
25*2d57dc69SVladimir Kotalfrom shutil import which
26*2d57dc69SVladimir Kotal
27*2d57dc69SVladimir Kotalfrom .repository import Repository, RepositoryException
28*2d57dc69SVladimir Kotalfrom ..utils.command import Command
29*2d57dc69SVladimir Kotal
30*2d57dc69SVladimir Kotal
31*2d57dc69SVladimir Kotalclass GitRepository(Repository):
32*2d57dc69SVladimir Kotal    def __init__(self, logger, path, project, command, env, hooks, timeout):
33*2d57dc69SVladimir Kotal        super().__init__(logger, path, project, command, env, hooks, timeout)
34*2d57dc69SVladimir Kotal
35*2d57dc69SVladimir Kotal        self.command = self._repository_command(command, default=lambda: which('git'))
36*2d57dc69SVladimir Kotal
37*2d57dc69SVladimir Kotal        if not self.command:
38*2d57dc69SVladimir Kotal            raise RepositoryException("Cannot get git command")
39*2d57dc69SVladimir Kotal
40*2d57dc69SVladimir Kotal        # The incoming() check relies on empty output so configure
41*2d57dc69SVladimir Kotal        # the repository first to avoid getting extra output.
42*2d57dc69SVladimir Kotal        git_command = [self.command, "config", "--local", "pull.ff", "only"]
43*2d57dc69SVladimir Kotal        cmd = self.getCommand(git_command, work_dir=self.path,
44*2d57dc69SVladimir Kotal                              env_vars=self.env, logger=self.logger)
45*2d57dc69SVladimir Kotal        cmd.execute()
46*2d57dc69SVladimir Kotal        if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED:
47*2d57dc69SVladimir Kotal            cmd.log_error("failed to configure git pull.ff")
48*2d57dc69SVladimir Kotal
49*2d57dc69SVladimir Kotal    def reposync(self):
50*2d57dc69SVladimir Kotal        return self._run_custom_sync_command([self.command, 'pull', '--ff-only'])
51*2d57dc69SVladimir Kotal
52*2d57dc69SVladimir Kotal    def incoming_check(self):
53*2d57dc69SVladimir Kotal        return self._run_custom_incoming_command([self.command, 'pull', '--dry-run'])
54