merge from upstream

This commit is contained in:
Serj Zavadsky committed 2019-01-29 16:46:09 +01:00
commit a31c92754f
136 files changed
+2167 -321

No files matched your search

+12 -6
View File
@@ -1,31 +1,37 @@
sudo: false
language: python
dist: xenial
matrix:
include:
- python: 2.7
env: TOXENV=py27-django110
- python: 2.7
env: TOXENV=py27-django111
- python: 3.4
env: TOXENV=py34-django110
- python: 3.4
env: TOXENV=py34-django111
- python: 3.4
env: TOXENV=py34-django20
- python: 3.5
env: TOXENV=py35-django110
- python: 3.5
env: TOXENV=py35-django111
- python: 3.5
env: TOXENV=py35-django20
- python: 3.5
env: TOXENV=py35-django21
- python: 3.6
env: TOXENV=py36-django111
- python: 3.6
env: TOXENV=py36-django20
- python: 3.6
env: TOXENV=py36-django21
- python: 3.7
env: TOXENV=py37-django111
- python: 3.7
env: TOXENV=py37-django20
- python: 3.7
env: TOXENV=py37-django21
- python: 3.6
env: TOXENV=lint
+44 -3
View File
@@ -44,12 +44,54 @@ Usage
``--exclude-apps EXCLUDE_APPS [EXCLUDE_APPS ...]`` Ignore migrations that are in the specified django apps.
``--verbose or -v`` Print more information during execution.
``--database DATABASE`` Specify the database for which to generate the SQL. Defaults to *default*.
``--cache-path PATH`` specify a directory that should be used to store cache-files in.
``--no-cache`` Don't use a cache.
================================================== ===========================================================================================================================
Examples
--------
3YOURMIND is running the linter on `Bitbucket Pipelines`_ on every build getting pushed.
3YOURMIND is running the linter on every build getting pushed through CI.
Checkout the ``examples/`` folder to see how you could integrate the linter in your test suite.
Backward incompatible migrations
--------------------------------
The linter analyses your migrations and checks the SQL for:
- Added ``NOT NULL`` columns, which don't have a DEFAULT value
- Dropping columns
- Renaming columns
- Renaming tables
- Altering columns (which can be backward compatible and eventually ignored)
Those are the most important and frequent backward incompatible migrations. We are happy to add more if you have some.
Ignoring migrations
-------------------
You can also ignore migrations by adding this to your migrations:
.. code-block::
import django_migration_linter as linter
# ...
operations = [
linter.IgnoreMigration(),
# ...
]
# ...
Cache
-----
By default, the linter uses a cache to prevent linting the same migration again.
The default location of the cache on Linux is
``/home/<username>/.cache/django-migration-linter/<version>/<ldjango-project>.pickle``.
Since the linter uses hashes, modifying migration files, will re-run the linter on that migration.
If you want to run the linter without cache, use the flag ``--no-cache``.
If you want to invalidate the cache, delete the cache folder.
Tests
-----
@@ -60,7 +102,7 @@ Contributing
------------
First, thank you very much if you want to contribute to the project.
Please base your work on the ``dev`` branch and also target this branch in your pull request.
Please base your work on the ``master`` branch and also target this branch in your pull request.
License
-------
@@ -68,6 +110,5 @@ License
*django-migration-linter* is released under the `Apache 2.0 License`_.
.. _`Bitbucket Pipelines`: https://bitbucket.org/product/features/pipelines
.. _`tox`: https://pypi.python.org/pypi/tox
.. _`Apache 2.0 License`: https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE
+2 -1
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,3 +13,4 @@
# limitations under the License.
from .migration_linter import * # noqa
from .operations import * # noqa
+50
View File
@@ -0,0 +1,50 @@
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hashlib
import os
import pickle
from .utils import split_path
class Cache(dict):
def __init__(self, django_folder, cache_path):
self.django_folder = django_folder
project_name = split_path(django_folder)[-1]
self.filename = os.path.join(cache_path, "{0}.pickle".format(project_name))
if not os.path.exists(os.path.dirname(self.filename)):
os.makedirs(os.path.dirname(self.filename))
super(Cache, self).__init__()
def load(self):
try:
with open(self.filename, "rb") as f:
tmp_dict = pickle.load(f)
self.update(tmp_dict)
except IOError:
pass
def save(self):
with open(self.filename, "wb") as f:
pickle.dump(self, f, protocol=2)
def md5(self, path):
hash_md5 = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
+21
View File
@@ -0,0 +1,21 @@
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from appdirs import user_cache_dir
__version__ = "0.1.2"
MIGRATION_FOLDER_NAME = "migrations"
DEFAULT_CACHE_PATH = user_cache_dir("django-migration-linter", version=__version__)
+12
View File
@@ -0,0 +1,12 @@
from django_migration_linter.utils import split_migration_path
class Migration(object):
def __init__(self, abs_path):
self.abs_path = abs_path
self.app_name, self.name = split_migration_path(self.abs_path)
def __lt__(self, other):
if not isinstance(other, Migration):
return NotImplemented
return self.abs_path < other.abs_path
+202 -145
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -18,35 +18,47 @@ import os
import re
from subprocess import Popen, PIPE
import sys
from . import utils
from .cache import Cache
from .constants import DEFAULT_CACHE_PATH, MIGRATION_FOLDER_NAME
from .migration import Migration
from .utils import is_directory, is_django_project, clean_bytes_to_str
from .sql_analyser import analyse_sql_statements
log = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
class MigrationLinter(object):
MIGRATION_FOLDER_NAME = 'migrations'
def __init__(self, project_path, **kwargs):
# Verify correctness
if not utils.is_directory(project_path):
if not is_directory(project_path):
raise ValueError(
'The given path {0} does not seem to be a directory.'.format(
project_path))
if not utils.is_django_project(project_path):
"The given path {0} does not seem to be a directory.".format(
project_path
)
)
if not is_django_project(project_path):
raise ValueError(
('The given path {0} does not '
'seem to be a django project.').format(project_path))
("The given path {0} does not " "seem to be a django project.").format(
project_path
)
)
# Store parameters and options
self.django_path = project_path
self.ignore_name_contains = kwargs.get('ignore_name_contains', None)
self.ignore_name = kwargs.get('ignore_name', None) or tuple()
self.include_apps = kwargs.get('include_apps', None)
self.exclude_apps = kwargs.get('exclude_apps', None)
self.database = kwargs.get('database', None) or 'default'
self.python_exe = '{0}/bin/{1}'.format(sys.prefix, 'python') if \
hasattr(sys, 'real_prefix') else 'python'
self.django_path = os.path.abspath(project_path)
self.ignore_name_contains = kwargs.get("ignore_name_contains", None)
self.ignore_name = kwargs.get("ignore_name", None) or tuple()
self.include_apps = kwargs.get("include_apps", None)
self.exclude_apps = kwargs.get("exclude_apps", None)
self.database = kwargs.get("database", None) or "default"
self.cache_path = kwargs.get("cache_path", None) or DEFAULT_CACHE_PATH
self.no_cache = kwargs.get("no_cache", None) or False
self.python_exe = (
"{0}/bin/{1}".format(sys.prefix, "python")
if hasattr(sys, "real_prefix")
else "python"
)
# Initialise counters
self.nb_valid = 0
@@ -54,62 +66,96 @@ class MigrationLinter(object):
self.nb_erroneous = 0
self.nb_total = 0
def lint_migration(self, app_name, migration_name):
print('({0}, {1})... '.format(app_name, migration_name), end='')
# Initialise cache. Read from old, write to new to prune old entries.
self.old_cache = Cache(self.django_path, self.cache_path)
self.new_cache = Cache(self.django_path, self.cache_path)
if not self.no_cache:
self.old_cache.load()
def lint_migration(self, migration):
app_name = migration.app_name
migration_name = migration.name
print("({0}, {1})... ".format(app_name, migration_name), end="")
self.nb_total += 1
md5hash = self.old_cache.md5(migration.abs_path)
if md5hash in self.old_cache:
if self.old_cache[md5hash]["result"] == "IGNORE":
print("IGNORE (cached)")
self.nb_ignored += 1
elif self.old_cache[md5hash]["result"] == "OK":
print("OK (cached)")
self.nb_valid += 1
else:
print("ERR (cached)")
self.nb_erroneous += 1
if "errors" in self.old_cache[md5hash]:
self.print_errors(self.old_cache[md5hash]["errors"])
self.new_cache[md5hash] = self.old_cache[md5hash]
return
if self.should_ignore_migration(app_name, migration_name):
print('IGNORE')
print("IGNORE")
self.nb_ignored += 1
return
sql_statements = self.get_sql(app_name, migration_name)
analysis_result = analyse_sql_statements(sql_statements)
errors = analysis_result["errors"]
if analysis_result["ignored"]:
print("IGNORE")
self.new_cache[md5hash] = {"result": "IGNORE"}
self.nb_ignored += 1
return
sql_statements = self.get_sql(
app_name, migration_name)
analysis_result = analyse_sql_statements(
sql_statements)
errors = analysis_result['errors']
if not errors:
print('OK')
print("OK")
self.new_cache[md5hash] = {"result": "OK"}
self.nb_valid += 1
return
# Print errors
print('ERR')
print("ERR")
self.new_cache[md5hash] = {"result": "ERR", "errors": errors}
self.nb_erroneous += 1
self.print_errors(errors)
@staticmethod
def print_errors(errors):
for err in errors:
error_str = '\t{0}'.format(err['err_msg'])
if err['table']:
error_str += ' (table: {0}'.format(err['table'])
if err['column']:
error_str += ', column: {0}'.format(
err['column'])
error_str += ')'
error_str = "\t{0}".format(err["err_msg"])
if err["table"]:
error_str += " (table: {0}".format(err["table"])
if err["column"]:
error_str += ", column: {0}".format(err["column"])
error_str += ")"
print(error_str)
def lint_all_migrations(self, git_commit_id=None):
# Collect migrations
if git_commit_id:
if not utils.is_git_project(self.django_path):
raise ValueError(
('The given project {0} does not seem '
'to be versioned by git.').format(self.django_path))
migrations = self._gather_migrations_git(git_commit_id)
else:
migrations = self._gather_all_migrations()
# Lint those migrations
for m in migrations:
self.lint_migration(*m)
self.lint_migration(m)
if not self.no_cache:
self.new_cache.save()
def print_summary(self):
print('*** Summary:')
print(('Valid migrations: {1}/{0} - '
'erroneous migrations: {2}/{0} - '
'ignored migrations: {3}/{0}').format(
self.nb_total,
self.nb_valid,
self.nb_erroneous,
self.nb_ignored))
print("*** Summary:")
print(
(
"Valid migrations: {1}/{0} - "
"erroneous migrations: {2}/{0} - "
"ignored migrations: {3}/{0}"
).format(self.nb_total, self.nb_valid, self.nb_erroneous, self.nb_ignored)
)
@property
def has_errors(self):
@@ -128,148 +174,157 @@ class MigrationLinter(object):
it allows to seperate the instances correctly.
"""
sqlmigrate_command = (
'cd {0} && '
'{1} manage.py sqlmigrate {2} {3} '
'--database {4}').format(
self.django_path,
self.python_exe, app_name, migration_name,
self.database)
log.info('Executing {0}'.format(sqlmigrate_command))
"cd {0} && {1} manage.py sqlmigrate {2} {3} " "--database {4}"
).format(
self.django_path, self.python_exe, app_name, migration_name, self.database
)
logger.info("Executing {0}".format(sqlmigrate_command))
sqlmigrate_process = Popen(
sqlmigrate_command, shell=True, stdout=PIPE, stderr=PIPE)
sqlmigrate_command, shell=True, stdout=PIPE, stderr=PIPE
)
sql_statements = []
for line in map(
utils.clean_bytes_to_str,
sqlmigrate_process.stdout.readlines()):
for line in map(clean_bytes_to_str, sqlmigrate_process.stdout.readlines()):
sql_statements.append(line)
sqlmigrate_process.wait()
if sqlmigrate_process.returncode != 0:
_, err = sqlmigrate_process.communicate()
raise RuntimeError('sqlmigrate command failed {0}'.format(
err.decode('utf-8')))
log.info('Found {0} sql migration lines'.format(len(sql_statements)))
raise RuntimeError(
"sqlmigrate command failed {0}".format(err.decode("utf-8"))
)
logger.info("Found {0} sql migration lines".format(len(sql_statements)))
return sql_statements
def _gather_migrations_git(self, git_commit_id):
migrations = []
# Get changes since specified commit
git_diff_command = (
'cd {0} && '
'git diff --name-only --diff-filter=A {1}').format(
self.django_path, git_commit_id)
log.info('Executing {0}'.format(git_diff_command))
diff_process = Popen(
git_diff_command, shell=True, stdout=PIPE, stderr=PIPE)
for line in map(
utils.clean_bytes_to_str, diff_process.stdout.readlines()):
"cd {0} && git diff --relative --name-only --diff-filter=A {1}"
).format(self.django_path, git_commit_id)
logger.info("Executing {0}".format(git_diff_command))
diff_process = Popen(git_diff_command, shell=True, stdout=PIPE, stderr=PIPE)
for line in map(clean_bytes_to_str, diff_process.stdout.readlines()):
# Only gather lines that include added migrations
if re.search(
'\/{0}\/.*\.py'.format(self.MIGRATION_FOLDER_NAME),
line) and \
'__init__' not in line:
app_name, migration_name = self._split_migration_path(line)
migrations.append((app_name, migration_name))
if (
re.search(r"\/{0}\/.*\.py".format(MIGRATION_FOLDER_NAME), line)
and "__init__" not in line
):
migrations.append(Migration(os.path.join(self.django_path, line)))
diff_process.wait()
if diff_process.returncode != 0:
output = []
for line in map(
utils.clean_bytes_to_str, diff_process.stderr.readlines()):
for line in map(clean_bytes_to_str, diff_process.stderr.readlines()):
output.append(line)
log.info("Error while git diff command:\n{}".format(
"".join(output)))
raise Exception('Error while executing git diff command')
logger.error("Error while git diff command:\n{}".format("".join(output)))
raise Exception("Error while executing git diff command")
return migrations
def _gather_all_migrations(self):
migrations = []
for root, dirs, files in os.walk(self.django_path):
for file_name in files:
if os.path.basename(root) == self.MIGRATION_FOLDER_NAME and \
file_name.endswith('.py') and \
file_name != '__init__.py':
for file_name in sorted(files):
if (
os.path.basename(root) == MIGRATION_FOLDER_NAME
and file_name.endswith(".py")
and file_name != "__init__.py"
):
full_migration_path = os.path.join(root, file_name)
app_name, migration_name = self._split_migration_path(
full_migration_path)
migrations.append((app_name, migration_name))
migrations.append(Migration(full_migration_path))
return migrations
def should_ignore_migration(self, app_name, migration_name):
return (self.include_apps and
app_name not in self.include_apps)\
or (self.exclude_apps and
app_name in self.exclude_apps)\
or (self.ignore_name_contains and
self.ignore_name_contains in migration_name)\
return (
(self.include_apps and app_name not in self.include_apps)
or (self.exclude_apps and app_name in self.exclude_apps)
or (
self.ignore_name_contains
and self.ignore_name_contains in migration_name
)
or (migration_name in self.ignore_name)
@classmethod
def _split_migration_path(cls, migration_path):
decomposed_path = utils.split_path(migration_path)
for i, p in enumerate(decomposed_path):
if p == cls.MIGRATION_FOLDER_NAME:
return (decomposed_path[i-1],
os.path.splitext(decomposed_path[i+1])[0])
)
def _main():
import argparse
parser = argparse.ArgumentParser(
description='Detect backward incompatible django migrations.')
description="Detect backward incompatible django migrations."
)
parser.add_argument(
'django_folder',
metavar='DJANGO_FOLDER',
"django_folder",
metavar="DJANGO_FOLDER",
type=str,
nargs=1,
help='the path to the django project')
help="the path to the django project",
)
parser.add_argument(
'commit_id',
metavar='GIT_COMMIT_ID',
type=str, nargs='?',
help=('if specified, only migrations since this commit '
'will be taken into account. If not specified, '
'the initial repo commit will be used'))
parser.add_argument(
'--ignore-name-contains',
"commit_id",
metavar="GIT_COMMIT_ID",
type=str,
nargs='?',
help='ignore migrations containing this name')
nargs="?",
help=(
"if specified, only migrations since this commit "
"will be taken into account. If not specified, "
"the initial repo commit will be used"
),
)
parser.add_argument(
'--ignore-name',
"--ignore-name-contains",
type=str,
nargs='*',
help='ignore migrations with exactly one of these names')
nargs="?",
help="ignore migrations containing this name",
)
parser.add_argument(
'--verbose',
'-v',
action='store_true',
help='print more information during execution')
parser.add_argument(
'--database',
"--ignore-name",
type=str,
nargs='?',
help=('specify the database for which to generate the SQL. '
'Defaults to default'))
nargs="*",
help="ignore migrations with exactly one of these names",
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="print more information during execution",
)
parser.add_argument(
"--database",
type=str,
nargs="?",
help=(
"specify the database for which to generate the SQL. " "Defaults to default"
),
)
incl_excl_group = parser.add_mutually_exclusive_group(
required=False)
incl_excl_group.add_argument(
'--include-apps',
cache_group = parser.add_mutually_exclusive_group(required=False)
cache_group.add_argument(
"--cache-path",
type=str,
nargs='*',
help='check only migrations that are in the specified django apps')
help="specify a directory that should be used to" "store cache-files in.",
)
cache_group.add_argument(
"--no-cache", action="store_true", help="don't use a cache"
)
incl_excl_group = parser.add_mutually_exclusive_group(required=False)
incl_excl_group.add_argument(
'--exclude-apps',
"--include-apps",
type=str,
nargs='*',
help='ignore migrations that are in the specified django apps')
nargs="*",
help="check only migrations that are in the specified django apps",
)
incl_excl_group.add_argument(
"--exclude-apps",
type=str,
nargs="*",
help="ignore migrations that are in the specified django apps",
)
args = parser.parse_args()
if args.verbose:
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
else:
logging.basicConfig(format='%(message)s')
logging.basicConfig(format="%(message)s")
folder_name = args.django_folder[0]
# Create and use linter
@@ -279,13 +334,15 @@ def _main():
ignore_name=args.ignore_name,
include_apps=args.include_apps,
exclude_apps=args.exclude_apps,
database=args.database)
linter.lint_all_migrations(
git_commit_id=args.commit_id)
database=args.database,
cache_path=args.cache_path,
no_cache=args.no_cache,
)
linter.lint_all_migrations(git_commit_id=args.commit_id)
linter.print_summary()
if linter.has_errors:
sys.exit(1)
if __name__ == '__main__':
if __name__ == "__main__":
_main()
+36
View File
@@ -0,0 +1,36 @@
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.db.migrations.operations.base import Operation
IGNORE_MIGRATION_SQL = "-- dml ignores this migration"
class IgnoreMigration(Operation):
reversible = True
# Must be true to be generated through the sqlmigrate command
reduces_to_sql = True
def state_forwards(self, app_label, state):
pass
def database_forwards(self, app_label, schema_editor, from_state, to_state):
schema_editor.execute(IGNORE_MIGRATION_SQL)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
schema_editor.execute(IGNORE_MIGRATION_SQL + "(reversed)")
def describe(self):
return "The Django migration linter will ignore this migration"
+71 -55
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -15,75 +15,91 @@
import re
import logging
log = logging.getLogger(__name__)
from .operations import IGNORE_MIGRATION_SQL
IGNORED_MIGRATION = "IGNORED_MIGRATION"
logger = logging.getLogger(__name__)
def has_default(sql, **kwargs):
if re.search('SET DEFAULT', sql) and kwargs['errors']:
err = next(err
for err in kwargs['errors']
if err['code'] == 'NOT_NULL')
if re.search("SET DEFAULT", sql) and kwargs["errors"]:
err = next((err for err in kwargs["errors"] if err["code"] == "NOT_NULL"), None)
if err:
log.info(
('Found a NOT_NULL error in migration, '
'but it has a default value added: {}').format(err))
kwargs['errors'].remove(err)
logger.info(
(
"Found a NOT_NULL error in migration, "
"but it has a default value added: {}"
).format(err)
)
kwargs["errors"].remove(err)
return False # Never fails
migration_tests = (
{
'code': 'NOT_NULL',
'fn': lambda sql, **kw: re.search('NOT NULL', sql) and
not re.search('CREATE TABLE', sql),
'err_msg': 'NOT NULL constraint on columns'
}, {
'code': 'DROP_COLUMN',
'fn': lambda sql, **kw: re.search('DROP COLUMN', sql),
'err_msg': 'DROPPING columns'
}, {
'code': 'RENAME_COLUMN',
'fn': lambda sql, **kw: re.search('ALTER TABLE .* CHANGE', sql) or
re.search('ALTER TABLE .* RENAME COLUMN', sql),
'err_msg': 'RENAMING columns'
}, {
'code': 'RENAME_TABLE',
'fn': lambda sql, **kw: re.search('RENAME TABLE', sql) or
re.search('ALTER TABLE .* RENAME TO', sql),
'err_msg': 'RENAMING tables'
}, {
'code': 'ALTER_COLUMN',
'fn': lambda sql, **kw: re.search('ALTER TABLE .* MODIFY', sql),
'err_msg': ('ALTERING columns (Could be backward compatible. '
'You may ignore this migration.)')
}, {
'code': '',
'fn': has_default,
'err_msg': ''
}
"code": "NOT_NULL",
"fn": lambda sql, **kw: re.search("NOT NULL", sql)
and not re.search("CREATE TABLE", sql),
"err_msg": "NOT NULL constraint on columns",
},
{
"code": "DROP_COLUMN",
"fn": lambda sql, **kw: re.search("DROP COLUMN", sql),
"err_msg": "DROPPING columns",
},
{
"code": "RENAME_COLUMN",
"fn": lambda sql, **kw: re.search("ALTER TABLE .* CHANGE", sql)
or re.search("ALTER TABLE .* RENAME COLUMN", sql),
"err_msg": "RENAMING columns",
},
{
"code": "RENAME_TABLE",
"fn": lambda sql, **kw: re.search("RENAME TABLE", sql)
or re.search("ALTER TABLE .* RENAME TO", sql),
"err_msg": "RENAMING tables",
},
{
"code": "ALTER_COLUMN",
"fn": lambda sql, **kw: re.search("ALTER TABLE .* MODIFY", sql),
"err_msg": (
"ALTERING columns (Could be backward compatible. "
"You may ignore this migration.)"
),
},
{"code": "", "fn": has_default, "err_msg": ""},
{
"code": IGNORED_MIGRATION,
"fn": lambda sql, **kw: re.search(IGNORE_MIGRATION_SQL, sql),
"err_msg": "",
},
)
def analyse_sql_statements(sql_statements):
errors = []
ignored = False
for statement in sql_statements:
for test in migration_tests:
if test['fn'](statement, errors=errors):
log.info('Testing {0} -- ERROR'.format(statement))
table_search = re.search(
'TABLE `([^`]*)`', statement, re.IGNORECASE)
col_search = re.search(
'COLUMN `([^`]*)`', statement, re.IGNORECASE)
err = {
'err_msg': test['err_msg'],
'code': test['code'],
'table': table_search.group(1) if table_search else None,
'column': col_search.group(1) if col_search else None
}
errors.append(err)
if test["fn"](statement, errors=errors):
if test["code"] == IGNORED_MIGRATION:
logger.info("Testing {0} -- IGNORING MIGRATION".format(statement))
ignored = True
else:
logger.info("Testing {0} -- ERROR".format(statement))
table_search = re.search(
"TABLE `([^`]*)`", statement, re.IGNORECASE
)
col_search = re.search("COLUMN `([^`]*)`", statement, re.IGNORECASE)
err = {
"err_msg": test["err_msg"],
"code": test["code"],
"table": table_search.group(1) if table_search else None,
"column": col_search.group(1) if col_search else None,
}
errors.append(err)
else:
log.info('Testing {0} -- PASSED'.format(statement))
return {
'errors': errors,
}
logger.info("Testing {0} -- PASSED".format(statement))
return {"errors": errors, "ignored": ignored}
+26 -13
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,20 +13,18 @@
# limitations under the License.
from __future__ import print_function
import os
import sys
from .constants import MIGRATION_FOLDER_NAME
def is_django_project(path):
django_manage_file = os.path.join(path, 'manage.py')
django_manage_file = os.path.join(path, "manage.py")
return os.path.isfile(django_manage_file)
def is_git_project(path):
git_directory = os.path.join(path, '.git')
return os.path.isdir(git_directory)
def is_directory(path):
return os.path.isdir(path)
@@ -41,16 +39,31 @@ def find_project_settings_module(path):
"""
for root, dirs, files in os.walk(path):
for file_name in files:
if file_name == 'settings.py':
return os.path.join(
root.replace(path, ''),
file_name).replace('/', '.').rstrip('.py')
if file_name == "settings.py":
return (
os.path.join(root.replace(path, ""), file_name)
.replace("/", ".")
.rstrip(".py")
)
def split_path(path):
a, b = os.path.split(path)
return (split_path(a) if (len(a) > 0 and a != '/') else []) + [b]
return (split_path(a) if (len(a) > 0 and a != "/") else []) + ([b] if b else [])
def split_migration_path(migration_path):
decomposed_path = split_path(migration_path)
for i, p in enumerate(decomposed_path):
if p == MIGRATION_FOLDER_NAME:
return (decomposed_path[i - 1], os.path.splitext(decomposed_path[i + 1])[0])
def compose_migration_path(django_folder, app_name, migration):
return os.path.join(
django_folder, app_name, MIGRATION_FOLDER_NAME, "{0}.py".format(migration)
)
def clean_bytes_to_str(byte_input):
return byte_input.decode('utf-8').strip()
return byte_input.decode("utf-8").strip()
+1 -1
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
+20 -6
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -11,18 +11,30 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
import re
from os import path
from setuptools import setup, find_packages
PROJECT_DIR = path.abspath(path.dirname(__file__))
def get_version():
constants = path.join(PROJECT_DIR, 'django_migration_linter', 'constants.py')
_version_re = re.compile(r"__version__\s+=\s+(?P<version>.*)")
with open(constants, "r") as f:
match = _version_re.search(f.read())
version = match.group("version") if match is not None else '"unknown"'
return str(ast.literal_eval(version))
with open(path.join(PROJECT_DIR, 'README.rst')) as f:
long_description = f.read()
install_requirements = [
'django>=1.10',
'django>=1.11',
'appdirs==1.4.3'
]
test_requirements = [
@@ -31,9 +43,10 @@ test_requirements = [
'django-fake-database-backends',
]
# noinspection PyUnresolvedReferences
setup(
name='django-migration-linter',
version='0.0.5',
version=get_version(),
description='Detect backward incompatible migrations for your django project',
long_description=long_description,
@@ -54,13 +67,13 @@ setup(
keywords='django migration lint linter database backward compatibility',
classifiers=[
'Development Status :: 3 - Alpha',
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Framework :: Django :: 2.1',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
@@ -69,5 +82,6 @@ setup(
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
]
)
+13 -2
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -40,16 +40,27 @@ CORRECT_PROJECT = os.path.join(
_FIXTURES_FOLDER, 'test_correct_project/')
DELETED_MIGRATION_PROJECT = os.path.join(
_FIXTURES_FOLDER, 'test_deleted_migration_project/')
IGNORE_MIGRATION_PROJECT = os.path.join(
_FIXTURES_FOLDER, 'test_project_ignore_migration/')
NOT_DJANGO_GIT_PROJECT = os.path.join(
_FIXTURES_FOLDER, 'test_git_project/')
NOT_GIT_DJANGO_PROJECT = os.path.join(
_FIXTURES_FOLDER, 'test_django_without_git_project/')
NON_GIT_ROOT_GIT_FOLDER = os.path.join(
_FIXTURES_FOLDER, 'test_non_root_git_project/'
)
NON_GIT_ROOT_DJANGO_PROJECT = os.path.join(
NON_GIT_ROOT_GIT_FOLDER, 'django_project/'
)
ALL_GIT_PROJECTS = (
NOT_DJANGO_GIT_PROJECT,
MULTI_COMMIT_PROJECT,
DELETED_MIGRATION_PROJECT
DELETED_MIGRATION_PROJECT,
NON_GIT_ROOT_GIT_FOLDER,
)
+165
View File
@@ -0,0 +1,165 @@
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pprint
import shutil
import tempfile
import unittest
import sys
from tests import fixtures
from django_migration_linter import MigrationLinter, Cache, DEFAULT_CACHE_PATH
if sys.version_info >= (3, 3):
import unittest.mock as mock
else:
import mock
class CacheTest(unittest.TestCase):
MIGRATION_FILE = os.path.join(fixtures.ALTER_COLUMN_PROJECT, 'test_app', 'migrations', '0001_initial.py')
fd, temp_path = tempfile.mkstemp()
def setUp(self):
shutil.copy2(self.MIGRATION_FILE, self.temp_path)
def tearDown(self):
shutil.copy2(self.temp_path, self.MIGRATION_FILE)
os.remove(self.temp_path)
def test_cache_normal(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_add_not_null_column.pickle')
if os.path.exists(cache_file):
os.remove(cache_file)
linter = MigrationLinter(fixtures.ADD_NOT_NULL_COLUMN_PROJECT)
with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql)as sql_mock:
linter.lint_all_migrations()
self.assertEqual(sql_mock.call_count, 2)
cache = Cache(
fixtures.ADD_NOT_NULL_COLUMN_PROJECT,
DEFAULT_CACHE_PATH
)
cache.load()
self.assertEqual(cache['3ef74e7f3e53e273e2fc95379248d58d']['result'], 'OK')
self.assertEqual(cache['e1e312b6d08ecbe017c25c58fc2be257']['result'], 'ERR')
self.assertListEqual(
cache['e1e312b6d08ecbe017c25c58fc2be257']['errors'],
[{'err_msg': 'RENAMING tables', 'code': 'RENAME_TABLE', 'table': None, 'column': None}]
)
# Start the Linter again -> should use cache now.
linter = MigrationLinter(fixtures.ADD_NOT_NULL_COLUMN_PROJECT)
with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock:
linter.lint_all_migrations()
self.assertEqual(sql_mock.call_count, 0)
self.assertTrue(linter.has_errors)
def test_cache_ignored(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_ignore_migration.pickle')
if os.path.exists(cache_file):
os.remove(cache_file)
linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT)
with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock:
linter.lint_all_migrations()
self.assertEqual(sql_mock.call_count, 2)
cache = Cache(
fixtures.IGNORE_MIGRATION_PROJECT,
DEFAULT_CACHE_PATH
)
cache.load()
self.assertEqual(cache['63230606af0eccaef7f1f78c537c624c']['result'], 'OK')
self.assertEqual(cache['5c5ca1780a9f28439c1defc1f32af894']['result'], 'IGNORE')
# Start the Linter again -> should use cache now.
linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT)
with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock:
linter.lint_all_migrations()
self.assertEqual(sql_mock.call_count, 0)
def test_cache_ignored_command_line(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_ignore_migration.pickle')
if os.path.exists(cache_file):
os.remove(cache_file)
linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT,
ignore_name_contains='0001')
with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock:
linter.lint_all_migrations()
self.assertEqual(sql_mock.call_count, 1)
cache = Cache(
fixtures.IGNORE_MIGRATION_PROJECT,
DEFAULT_CACHE_PATH
)
cache.load()
self.assertNotIn('63230606af0eccaef7f1f78c537c624c', cache)
self.assertEqual(cache['5c5ca1780a9f28439c1defc1f32af894']['result'], 'IGNORE')
def test_cache_modified(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_alter_column.pickle')
if os.path.exists(cache_file):
os.remove(cache_file)
linter = MigrationLinter(fixtures.ALTER_COLUMN_PROJECT)
linter.lint_all_migrations()
cache = Cache(
fixtures.ALTER_COLUMN_PROJECT,
DEFAULT_CACHE_PATH
)
cache.load()
self.assertEqual(cache['8589aa107b6da296c4b49cd2681d2230']['result'], 'OK',
'If this fails, tearDown might have failed to remove '
'the modification from tests/test_project_fixtures/'
'test_project_alter_column/test_app/migrations/0001_initial.py'
)
self.assertEqual(cache['8f54c4a434cfaa9838e8ca12eb988255']['result'], 'ERR')
self.assertListEqual(
cache['8f54c4a434cfaa9838e8ca12eb988255']['errors'],
[{u'err_msg': u'ALTERING columns (Could be backward compatible. '
u'You may ignore this migration.)',
u'code': u'ALTER_COLUMN',
u'table': u'test_app_a',
u'column': None}
]
)
# Modify migration
with open(self.MIGRATION_FILE, "a") as f:
f.write("# modification at the end of the file\n")
# Start the Linter again -> Cache should look different now
linter = MigrationLinter(fixtures.ALTER_COLUMN_PROJECT)
linter.lint_all_migrations()
cache = Cache(
fixtures.ALTER_COLUMN_PROJECT,
DEFAULT_CACHE_PATH
)
cache.load()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(cache)
self.assertNotIn('8589aa107b6da296c4b49cd2681d2230', cache)
self.assertEqual(cache['44f9d6d019d6f158946c2819b2bd8bf9']['result'], 'OK')
+161 -31
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import shutil
import unittest
from subprocess import Popen, PIPE
from django_migration_linter import utils
from django_migration_linter import utils, DEFAULT_CACHE_PATH
from tests import fixtures
import sys
@@ -27,20 +29,26 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
cls.linter_exec = '{0}/bin/{1}'.format(sys.prefix, linter_name) if hasattr(sys, 'real_prefix') else linter_name
def test_call_linter_cmd_line_working(self):
cmd = '{0} {1}'.format(self.linter_exec, fixtures.CORRECT_PROJECT)
cmd = '{0} --no-cache {1}'.format(self.linter_exec, fixtures.CORRECT_PROJECT)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].endswith('OK'))
self.assertTrue(lines[1].endswith('OK'))
self.assertTrue(lines[2].endswith('OK'))
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... OK",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... OK",
])
)
def test_call_linter_cmd_line_errors(self):
cmd = '{0} {1}'.format(
cmd = '{0} --no-cache {1}'.format(
self.linter_exec,
fixtures.ADD_NOT_NULL_COLUMN_PROJECT)
@@ -50,13 +58,12 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
self.assertNotEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
print(lines)
self.assertTrue(lines[0].endswith('OK'))
self.assertTrue(lines[1].endswith('ERR'))
self.assertTrue('RENAMING tables' in lines[2])
def test_call_linter_cmd_line_exclude_apps(self):
cmd = '{0} {1} --exclude-apps test_app2'.format(
cmd = '{0} --no-cache {1} --exclude-apps test_app2'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT)
@@ -65,13 +72,19 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].endswith('OK'))
self.assertTrue(lines[1].endswith('OK'))
self.assertTrue(lines[2].endswith('IGNORE'))
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... OK",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... IGNORE",
"(test_app3, 0001_initial)... OK",
])
)
def test_call_linter_cmd_line_include_apps(self):
cmd = '{0} {1} --include-apps test_app2'.format(
cmd = '{0} --no-cache {1} --include-apps test_app2'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT)
@@ -80,13 +93,19 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].endswith('IGNORE'))
self.assertTrue(lines[1].endswith('IGNORE'))
self.assertTrue(lines[2].endswith('OK'))
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... IGNORE",
"(test_app1, 0002_a_new_null_field)... IGNORE",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... IGNORE",
])
)
def test_call_linter_cmd_line_ignore_name(self):
cmd = '{0} {1} --ignore-name 0001_initial'.format(
cmd = '{0} --no-cache {1} --ignore-name 0001_initial'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT)
@@ -95,13 +114,19 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].endswith('IGNORE'))
self.assertTrue(lines[1].endswith('OK'))
self.assertTrue(lines[2].endswith('OK'))
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... IGNORE",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... IGNORE",
])
)
def test_call_linter_cmd_line_ignore_name_contains(self):
cmd = '{0} {1} --ignore-name-contains 0001'.format(
cmd = '{0} --no-cache {1} --ignore-name-contains 0001'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT)
@@ -110,13 +135,64 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].endswith('IGNORE'))
self.assertTrue(lines[1].endswith('OK'))
self.assertTrue(lines[2].endswith('IGNORE'))
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... IGNORE",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... IGNORE",
"(test_app3, 0001_initial)... IGNORE",
])
)
def test_call_linter_cmd_line_cache(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_correct_project.pickle')
if os.path.exists(cache_file):
os.remove(cache_file)
cmd = '{0} {1}'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT
)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertTrue(os.path.exists(DEFAULT_CACHE_PATH))
def test_call_linter_cmd_line_cache_path(self):
if os.path.exists('/tmp/migration-linter-cache-tests/'):
shutil.rmtree('/tmp/migration-linter-cache-tests/')
cmd = '{0} {1} --cache-path={2}'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT,
'/tmp/migration-linter-cache-tests/'
)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
self.assertTrue(os.path.exists('/tmp/migration-linter-cache-tests'))
def test_call_linter_cmd_line_no_cache(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_correct_project.pickle')
if os.path.exists(cache_file):
os.remove(cache_file)
cmd = '{0} --no-cache {1}'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT
)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
self.assertFalse(os.path.exists(cache_file))
def test_call_linter_cmd_line_git_id(self):
cmd = '{0} {1} d7125d5f4f0cc9623f670a66c54f131acc50032d'.format(
cmd = '{0} --no-cache {1} d7125d5f4f0cc9623f670a66c54f131acc50032d'.format(
self.linter_exec,
fixtures.MULTI_COMMIT_PROJECT)
fixtures.prepare_git_project(fixtures.MULTI_COMMIT_PROJECT)
@@ -131,7 +207,7 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
self.assertTrue(lines[1].startswith('*** Summary'))
def test_call_linter_with_deleted_migrations(self):
cmd = '{0} {1} 154ecf6119325cc3b1f3f5a4e709bfbd61a4a4ba'.format(
cmd = '{0} --no-cache {1} 154ecf6119325cc3b1f3f5a4e709bfbd61a4a4ba'.format(
self.linter_exec,
fixtures.DELETED_MIGRATION_PROJECT)
fixtures.prepare_git_project(fixtures.DELETED_MIGRATION_PROJECT)
@@ -144,3 +220,57 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
self.assertEqual(len(lines), 3)
self.assertTrue(lines[0].endswith('OK'))
self.assertTrue(lines[1].startswith('*** Summary'))
def test_call_from_within_project(self):
cmd = 'cd {0} && {1} --no-cache .'.format(
fixtures.CORRECT_PROJECT,
self.linter_exec)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... OK",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... OK",
])
)
def test_call_project_non_git_root(self):
# could use tag: version_ok (but doesn't work in Travis)
cmd = '{0} --no-cache {1} 2c46f438a89972f9ce2d5151a825a5bfb7f7db4b'.format(
self.linter_exec,
fixtures.NON_GIT_ROOT_DJANGO_PROJECT)
fixtures.prepare_git_project(fixtures.NON_GIT_ROOT_GIT_FOLDER)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 3)
self.assertTrue(lines[0].endswith('OK'))
self.assertTrue(lines[1].startswith('*** Summary'))
def test_call_project_non_git_root_ko(self):
# could use tag: version_ko (but doesn't work in Travis)
cmd = '{0} --no-cache {1} 0140e142724fc58944797f9ddc2ebf964146339a'.format(
self.linter_exec,
fixtures.NON_GIT_ROOT_DJANGO_PROJECT)
fixtures.prepare_git_project(fixtures.NON_GIT_ROOT_GIT_FOLDER)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertNotEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].endswith('ERR'))
self.assertTrue(lines[2].endswith('OK'))
self.assertTrue(lines[3].startswith('*** Summary'))
+5 -1
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -56,6 +56,10 @@ class BackwardcompatibilityDetectionTest(unittest.TestCase):
test_project_path = fixtures.RENAME_TABLE_PROJECT
self._test_linter_finds_errors(test_project_path)
def test_ignore_migration(self):
test_project_path = fixtures.IGNORE_MIGRATION_PROJECT
self._test_linter_finds_no_errors(test_project_path)
def test_accept_not_null_column_followed_by_adding_default(self):
test_project_path = \
fixtures.ADD_NOT_NULL_COLUMN_FOLLOWED_BY_DEFAULT_PROJECT
+7 -5
View File
@@ -1,4 +1,4 @@
# Copyright 2018 3YOURMIND GmbH
# Copyright 2019 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,8 +13,10 @@
# limitations under the License.
import unittest
import os
from django_migration_linter import MigrationLinter
from django_migration_linter.migration import Migration
from tests import fixtures
@@ -24,10 +26,10 @@ class TestMultipleLinters(unittest.TestCase):
l2 = MigrationLinter(fixtures.RENAME_COLUMN_PROJECT)
l3 = MigrationLinter(fixtures.CORRECT_PROJECT)
l1.lint_migration('test_app', '0002')
l2.lint_migration('test_app', '0002')
l3.lint_migration('test_app1', '0001')
l3.lint_migration('test_app1', '0002')
l1.lint_migration(Migration(os.path.join(fixtures.ADD_NOT_NULL_COLUMN_PROJECT, 'test_app/migrations/0002_add_new_not_null_field.py')))
l2.lint_migration(Migration(os.path.join(fixtures.RENAME_COLUMN_PROJECT, 'test_app/migrations/0002_rename_column.py')))
l3.lint_migration(Migration(os.path.join(fixtures.CORRECT_PROJECT, 'test_app1/migrations/0001_initial.py')))
l3.lint_migration(Migration(os.path.join(fixtures.CORRECT_PROJECT, 'test_app1/migrations/0002_a_new_null_field.py')))
self.assertTrue(l1.has_errors)
self.assertTrue(l2.has_errors)
Whitespace-only changes.
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
@@ -0,0 +1,5 @@
from django.apps import AppConfig
class TestApp3Config(AppConfig):
name = 'test_app3'
@@ -0,0 +1,21 @@
# Generated by Django 2.1.4 on 2019-01-04 20:55
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='C',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('not_null_field', models.IntegerField()),
],
),
]
@@ -0,0 +1,4 @@
from django.db import models
class C(models.Model):
not_null_field = models.IntegerField(null=False)
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.
@@ -11,10 +11,12 @@ https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
@@ -38,7 +40,8 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'test_app1',
'test_app2'
'test_app2',
'apps.test_app3',
]
MIDDLEWARE = [
@@ -0,0 +1,22 @@
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
@@ -0,0 +1,5 @@
from django.apps import AppConfig
class TestApp1Config(AppConfig):
name = 'test_app1'
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-20 10:31
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='A',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('null_field', models.IntegerField()),
],
),
]
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-20 10:32
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('test_app1', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='a',
name='null_field_2',
field=models.IntegerField(default=1),
),
]
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-20 10:33
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('test_app1', '0002_a_null_field_2'),
]
operations = [
migrations.AddField(
model_name='a',
name='non_null_field',
field=models.IntegerField(null=True),
),
]
@@ -0,0 +1,6 @@
from django.db import models
class A(models.Model):
null_field = models.IntegerField()
null_field_2 = models.IntegerField(default=1)
non_null_field = models.IntegerField(null=True)
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.
@@ -0,0 +1,122 @@
"""
Django settings for test_project project.
Generated by 'django-admin startproject' using Django 1.11.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'gtb+mohs%gf0+#fr89vw7e&#edmv=q8!iv+n&j_soawk$tkj3^'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test_app1',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'test_project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'test_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django_fake_database_backends.backends.mysql'
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
@@ -0,0 +1,21 @@
"""linter_test_project URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
@@ -0,0 +1,16 @@
"""
WSGI config for linter_test_project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")
application = get_wsgi_application()
@@ -0,0 +1 @@
Add ok migration
@@ -0,0 +1 @@
ref: refs/heads/master
@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
@@ -0,0 +1,15 @@
#!/bin/sh
#
# An example hook script to check the commit log message taken by
# applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit. The hook is
# allowed to edit the commit message file.
#
# To enable this hook, rename this file to "applypatch-msg".
. git-sh-setup
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
:
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".
# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
# This example catches duplicate Signed-off-by lines.
test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
@@ -0,0 +1,114 @@
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open2;
# An example hook script to integrate Watchman
# (https://facebook.github.io/watchman/) with git to speed up detecting
# new and modified files.
#
# The hook is passed a version (currently 1) and a time in nanoseconds
# formatted as a string and outputs to stdout all files that have been
# modified since the given time. Paths must be relative to the root of
# the working tree and separated by a single NUL.
#
# To enable this hook, rename this file to "query-watchman" and set
# 'git config core.fsmonitor .git/hooks/query-watchman'
#
my ($version, $time) = @ARGV;
# Check the hook interface version
if ($version == 1) {
# convert nanoseconds to seconds
$time = int $time / 1000000000;
} else {
die "Unsupported query-fsmonitor hook version '$version'.\n" .
"Falling back to scanning...\n";
}
my $git_work_tree;
if ($^O =~ 'msys' || $^O =~ 'cygwin') {
$git_work_tree = Win32::GetCwd();
$git_work_tree =~ tr/\\/\//;
} else {
require Cwd;
$git_work_tree = Cwd::cwd();
}
my $retry = 1;
launch_watchman();
sub launch_watchman {
my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty')
or die "open2() failed: $!\n" .
"Falling back to scanning...\n";
# In the query expression below we're asking for names of files that
# changed since $time but were not transient (ie created after
# $time but no longer exist).
#
# To accomplish this, we're using the "since" generator to use the
# recency index to select candidate nodes and "fields" to limit the
# output to file names only. Then we're using the "expression" term to
# further constrain the results.
#
# The category of transient files that we want to ignore will have a
# creation clock (cclock) newer than $time_t value and will also not
# currently exist.
my $query = <<" END";
["query", "$git_work_tree", {
"since": $time,
"fields": ["name"],
"expression": ["not", ["allof", ["since", $time, "cclock"], ["not", "exists"]]]
}]
END
print CHLD_IN $query;
close CHLD_IN;
my $response = do {local $/; <CHLD_OUT>};
die "Watchman: command returned no output.\n" .
"Falling back to scanning...\n" if $response eq "";
die "Watchman: command returned invalid output: $response\n" .
"Falling back to scanning...\n" unless $response =~ /^\{/;
my $json_pkg;
eval {
require JSON::XS;
$json_pkg = "JSON::XS";
1;
} or do {
require JSON::PP;
$json_pkg = "JSON::PP";
};
my $o = $json_pkg->new->utf8->decode($response);
if ($retry > 0 and $o->{error} and $o->{error} =~ m/unable to resolve root .* directory (.*) is not watched/) {
print STDERR "Adding '$git_work_tree' to watchman's watch list.\n";
$retry--;
qx/watchman watch "$git_work_tree"/;
die "Failed to make watchman watch '$git_work_tree'.\n" .
"Falling back to scanning...\n" if $? != 0;
# Watchman will always return all files on the first query so
# return the fast "everything is dirty" flag to git and do the
# Watchman query just to get it over with now so we won't pay
# the cost in git to look up each individual file.
print "/\0";
eval { launch_watchman() };
exit 0;
}
die "Watchman: $o->{error}.\n" .
"Falling back to scanning...\n" if $o->{error};
binmode STDOUT, ":utf8";
local $, = "\0";
print @{$o->{files}};
}
@@ -0,0 +1,8 @@
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
exec git update-server-info
@@ -0,0 +1,14 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed
# by applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-applypatch".
. git-sh-setup
precommit="$(git rev-parse --git-path hooks/pre-commit)"
test -x "$precommit" && exec "$precommit" ${1+"$@"}
:
@@ -0,0 +1,49 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --bool hooks.allownonascii)
# Redirect output to stderr.
exec 1>&2
# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit 1
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
@@ -0,0 +1,53 @@
#!/bin/sh
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
range="$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check for WIP commit
commit=`git rev-list -n 1 --grep '^WIP' "$range"`
if [ -n "$commit" ]
then
echo >&2 "Found WIP commit in $local_ref, not pushing"
exit 1
fi
fi
done
exit 0
@@ -0,0 +1,169 @@
#!/bin/sh
#
# Copyright (c) 2006, 2008 Junio C Hamano
#
# The "pre-rebase" hook is run just before "git rebase" starts doing
# its job, and can prevent the command from running by exiting with
# non-zero status.
#
# The hook is called with the following parameters:
#
# $1 -- the upstream the series was forked from.
# $2 -- the branch being rebased (or empty when rebasing the current branch).
#
# This sample shows how to prevent topic branches that are already
# merged to 'next' branch from getting rebased, because allowing it
# would result in rebasing already published history.
publish=next
basebranch="$1"
if test "$#" = 2
then
topic="refs/heads/$2"
else
topic=`git symbolic-ref HEAD` ||
exit 0 ;# we do not interrupt rebasing detached HEAD
fi
case "$topic" in
refs/heads/??/*)
;;
*)
exit 0 ;# we do not interrupt others.
;;
esac
# Now we are dealing with a topic branch being rebased
# on top of master. Is it OK to rebase it?
# Does the topic really exist?
git show-ref -q "$topic" || {
echo >&2 "No such branch $topic"
exit 1
}
# Is topic fully merged to master?
not_in_master=`git rev-list --pretty=oneline ^master "$topic"`
if test -z "$not_in_master"
then
echo >&2 "$topic is fully merged to master; better remove it."
exit 1 ;# we could allow it, but there is no point.
fi
# Is topic ever merged to next? If so you should not be rebasing it.
only_next_1=`git rev-list ^master "^$topic" ${publish} | sort`
only_next_2=`git rev-list ^master ${publish} | sort`
if test "$only_next_1" = "$only_next_2"
then
not_in_topic=`git rev-list "^$topic" master`
if test -z "$not_in_topic"
then
echo >&2 "$topic is already up to date with master"
exit 1 ;# we could allow it, but there is no point.
else
exit 0
fi
else
not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"`
/usr/bin/perl -e '
my $topic = $ARGV[0];
my $msg = "* $topic has commits already merged to public branch:\n";
my (%not_in_next) = map {
/^([0-9a-f]+) /;
($1 => 1);
} split(/\n/, $ARGV[1]);
for my $elem (map {
/^([0-9a-f]+) (.*)$/;
[$1 => $2];
} split(/\n/, $ARGV[2])) {
if (!exists $not_in_next{$elem->[0]}) {
if ($msg) {
print STDERR $msg;
undef $msg;
}
print STDERR " $elem->[1]\n";
}
}
' "$topic" "$not_in_next" "$not_in_master"
exit 1
fi
<<\DOC_END
This sample hook safeguards topic branches that have been
published from being rewound.
The workflow assumed here is:
* Once a topic branch forks from "master", "master" is never
merged into it again (either directly or indirectly).
* Once a topic branch is fully cooked and merged into "master",
it is deleted. If you need to build on top of it to correct
earlier mistakes, a new topic branch is created by forking at
the tip of the "master". This is not strictly necessary, but
it makes it easier to keep your history simple.
* Whenever you need to test or publish your changes to topic
branches, merge them into "next" branch.
The script, being an example, hardcodes the publish branch name
to be "next", but it is trivial to make it configurable via
$GIT_DIR/config mechanism.
With this workflow, you would want to know:
(1) ... if a topic branch has ever been merged to "next". Young
topic branches can have stupid mistakes you would rather
clean up before publishing, and things that have not been
merged into other branches can be easily rebased without
affecting other people. But once it is published, you would
not want to rewind it.
(2) ... if a topic branch has been fully merged to "master".
Then you can delete it. More importantly, you should not
build on top of it -- other people may already want to
change things related to the topic as patches against your
"master", so if you need further changes, it is better to
fork the topic (perhaps with the same name) afresh from the
tip of "master".
Let's look at this example:
o---o---o---o---o---o---o---o---o---o "next"
/ / / /
/ a---a---b A / /
/ / / /
/ / c---c---c---c B /
/ / / \ /
/ / / b---b C \ /
/ / / / \ /
---o---o---o---o---o---o---o---o---o---o---o "master"
A, B and C are topic branches.
* A has one fix since it was merged up to "next".
* B has finished. It has been fully merged up to "master" and "next",
and is ready to be deleted.
* C has not merged to "next" at all.
We would want to allow C to be rebased, refuse A, and encourage
B to be deleted.
To compute (1):
git rev-list ^master ^topic next
git rev-list ^master next
if these match, topic has not merged in next at all.
To compute (2):
git rev-list master..topic
if this is empty, it is fully merged to "master".
DOC_END
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to make use of push options.
# The example simply echoes all push options that start with 'echoback='
# and rejects all pushes when the "reject" push option is used.
#
# To enable this hook, rename this file to "pre-receive".
if test -n "$GIT_PUSH_OPTION_COUNT"
then
i=0
while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"
do
eval "value=\$GIT_PUSH_OPTION_$i"
case "$value" in
echoback=*)
echo "echo from the pre-receive-hook: ${value#*=}" >&2
;;
reject)
exit 1
esac
i=$((i + 1))
done
fi
@@ -0,0 +1,42 @@
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".
# This hook includes three examples. The first one removes the
# "# Please enter the commit message..." help message.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output. It is
# commented because it doesn't cope with --amend or with squashed
# commits.
#
# The third example adds a Signed-off-by line to the message, that can
# still be edited. This is rarely a good idea.
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
# case "$COMMIT_SOURCE,$SHA1" in
# ,|template,)
# /usr/bin/perl -i.bak -pe '
# print "\n" . `git diff --cached --name-status -r`
# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
# *) ;;
# esac
# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
# if test -z "$COMMIT_SOURCE"
# then
# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
# fi
@@ -0,0 +1,128 @@
#!/bin/sh
#
# An example hook script to block unannotated tags from entering.
# Called by "git receive-pack" with arguments: refname sha1-old sha1-new
#
# To enable this hook, rename this file to "update".
#
# Config
# ------
# hooks.allowunannotated
# This boolean sets whether unannotated tags will be allowed into the
# repository. By default they won't be.
# hooks.allowdeletetag
# This boolean sets whether deleting tags will be allowed in the
# repository. By default they won't be.
# hooks.allowmodifytag
# This boolean sets whether a tag may be modified after creation. By default
# it won't be.
# hooks.allowdeletebranch
# This boolean sets whether deleting branches will be allowed in the
# repository. By default they won't be.
# hooks.denycreatebranch
# This boolean sets whether remotely creating branches will be denied
# in the repository. By default this is allowed.
#
# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"
# --- Safety check
if [ -z "$GIT_DIR" ]; then
echo "Don't run this script from the command line." >&2
echo " (if you want, you could supply GIT_DIR then run" >&2
echo " $0 <ref> <oldrev> <newrev>)" >&2
exit 1
fi
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
echo "usage: $0 <ref> <oldrev> <newrev>" >&2
exit 1
fi
# --- Config
allowunannotated=$(git config --bool hooks.allowunannotated)
allowdeletebranch=$(git config --bool hooks.allowdeletebranch)
denycreatebranch=$(git config --bool hooks.denycreatebranch)
allowdeletetag=$(git config --bool hooks.allowdeletetag)
allowmodifytag=$(git config --bool hooks.allowmodifytag)
# check for no description
projectdesc=$(sed -e '1q' "$GIT_DIR/description")
case "$projectdesc" in
"Unnamed repository"* | "")
echo "*** Project description file hasn't been set" >&2
exit 1
;;
esac
# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
zero="0000000000000000000000000000000000000000"
if [ "$newrev" = "$zero" ]; then
newrev_type=delete
else
newrev_type=$(git cat-file -t $newrev)
fi
case "$refname","$newrev_type" in
refs/tags/*,commit)
# un-annotated tag
short_refname=${refname##refs/tags/}
if [ "$allowunannotated" != "true" ]; then
echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
exit 1
fi
;;
refs/tags/*,delete)
# delete tag
if [ "$allowdeletetag" != "true" ]; then
echo "*** Deleting a tag is not allowed in this repository" >&2
exit 1
fi
;;
refs/tags/*,tag)
# annotated tag
if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1
then
echo "*** Tag '$refname' already exists." >&2
echo "*** Modifying a tag is not allowed in this repository." >&2
exit 1
fi
;;
refs/heads/*,commit)
# branch
if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then
echo "*** Creating a branch is not allowed in this repository" >&2
exit 1
fi
;;
refs/heads/*,delete)
# delete branch
if [ "$allowdeletebranch" != "true" ]; then
echo "*** Deleting a branch is not allowed in this repository" >&2
exit 1
fi
;;
refs/remotes/*,commit)
# tracking branch
;;
refs/remotes/*,delete)
# delete tracking branch
if [ "$allowdeletebranch" != "true" ]; then
echo "*** Deleting a tracking branch is not allowed in this repository" >&2
exit 1
fi
;;
*)
# Anything else (is there anything else?)
echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
exit 1
;;
esac
# --- Finished
exit 0
@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
@@ -0,0 +1,6 @@
0000000000000000000000000000000000000000 30b505e031fc496c88d36be5ed7892337c640223 David Wobrock <david.wobrock@gmail.com> 1547980206 +0100 commit (initial): init
30b505e031fc496c88d36be5ed7892337c640223 a3f20003e5407a44000854c41ba29c06a3bbba82 David Wobrock <david.wobrock@gmail.com> 1547980230 +0100 commit (amend): init
a3f20003e5407a44000854c41ba29c06a3bbba82 0e52dc2380789e401cf385d3376af39f4bb6598f David Wobrock <david.wobrock@gmail.com> 1547980287 +0100 commit (amend): init
0e52dc2380789e401cf385d3376af39f4bb6598f 0140e142724fc58944797f9ddc2ebf964146339a David Wobrock <david.wobrock@gmail.com> 1547980306 +0100 commit: init migration
0140e142724fc58944797f9ddc2ebf964146339a 2c46f438a89972f9ce2d5151a825a5bfb7f7db4b David Wobrock <david.wobrock@gmail.com> 1547980356 +0100 commit: add ko migration
2c46f438a89972f9ce2d5151a825a5bfb7f7db4b f122245e6a3ce63b99e0beeb8403031edd08ed50 David Wobrock <david.wobrock@gmail.com> 1547980398 +0100 commit: Add ok migration
@@ -0,0 +1,6 @@
0000000000000000000000000000000000000000 30b505e031fc496c88d36be5ed7892337c640223 David Wobrock <david.wobrock@gmail.com> 1547980206 +0100 commit (initial): init
30b505e031fc496c88d36be5ed7892337c640223 a3f20003e5407a44000854c41ba29c06a3bbba82 David Wobrock <david.wobrock@gmail.com> 1547980230 +0100 commit (amend): init
a3f20003e5407a44000854c41ba29c06a3bbba82 0e52dc2380789e401cf385d3376af39f4bb6598f David Wobrock <david.wobrock@gmail.com> 1547980287 +0100 commit (amend): init
0e52dc2380789e401cf385d3376af39f4bb6598f 0140e142724fc58944797f9ddc2ebf964146339a David Wobrock <david.wobrock@gmail.com> 1547980306 +0100 commit: init migration
0140e142724fc58944797f9ddc2ebf964146339a 2c46f438a89972f9ce2d5151a825a5bfb7f7db4b David Wobrock <david.wobrock@gmail.com> 1547980356 +0100 commit: add ko migration
2c46f438a89972f9ce2d5151a825a5bfb7f7db4b f122245e6a3ce63b99e0beeb8403031edd08ed50 David Wobrock <david.wobrock@gmail.com> 1547980398 +0100 commit: Add ok migration
@@ -0,0 +1,2 @@
x¥�Á
!E[ûîƒáéÓ…ˆýCk}ê$åbõû}BËsáî¥ZJî\*±ë-FnÒL„h¼I6H��¤Á*oÑ&OJR¤ØÃµ¸uQË@␍,ÆF‚—Ù¥QPÞÏÚšÄܳ_kãg÷Ê�_ªo•nü¾8½xZ‹Ë÷‰j9r¡Õb␍ Ì|€�t íñËÛ¸ZòÚ\Ïuc�¿M1
@@ -0,0 +1,4 @@
x¥ŽM!ƒ]sŠ·7™ðó˜�ÄÞÁ5ð`$Ê`êõÅxÓÕ×´MC-%w�¨v½ÅÞ�CLA%3›”<YŒ
+—$”ñÑx=$‰=\‹[.�G�r‘£¤�Å‘]’%
2údg8+esÏ~­␍Îî• .Õ·np /NïžÖâò}
µAè1d¸Ò3ì¹àœ␍wíñ� æˆàV¡äµ¹žëÆ>P`M¼
@@ -0,0 +1,2 @@
x¥�;
1@­sŠé…eÆ$ÎD,¼ƒõä§A³�õú®xË÷Š÷B«µ  YoFO |¦¤9EC죓�mÊFŒql3û µ!Qò·Öá,¯áÒ|oá‡øÅéýÃÓµJyL¡Õ#�5ìfÜá¶Hˆjµëx¤?ª,e¨tð<|
@@ -0,0 +1 @@
x+)JMU0´0a040031Q0000ŒÏÌË,ÉLÌÑ+¨d˜½U´ä’}jZ$Ï·ÝsKd¸e?°G(5ŠOŒÏ+ÍɉOËLÍI‰7é½R¯¼™3uÁû´®�Ï,™™< ÖBè0éÈÏCÒÒÓ¿OšËÒ¿N1~η+}Ýoà“i€ê‰»&>¤ìÙÜG³7]¼æìÝ­¹®<êÆ¡'=Áå,JU
@@ -0,0 +1,2 @@
x]ŽÁ
Â0 @+zAð<î2«âQDðîÅ«XºµÎŒµ)m&úÝþ€™ò/!IÑ`1_/7bøšN.³Ã¹} ¸î9Óˆa„8e�9f ñ‡Ô²“7tV}#M­}…¹ƒ*jôyžl”d¥UˆXÛ’ÔÔFÛÛcìlßýŽêVò·+I¥À)µOóå­CÓ6v×ý—8ÅÇ!:L
Loaded 100 of 136 files, more files were not shown because too many files have changed in this diff. Show more