-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathseries_insert
More file actions
executable file
·143 lines (124 loc) · 4.86 KB
/
series_insert
File metadata and controls
executable file
·143 lines (124 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2018 SUSE LLC
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
"""
Script to insert new patches in series.conf according to the upstream order of
commits that the patches backport.
"""
import argparse
import collections
import sys
from git_sort import pygit2_wrapper as pygit2
from git_sort import series_conf
from git_sort import git_sort
from git_sort import lib
from suse_git import exc
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Insert new patches in series.conf according to the "
"upstream order of commits that the patches backport.")
parser.add_argument("patches", nargs="+", metavar="PATCH",
help="Patch file to insert in series.conf")
args = parser.parse_args()
repo_path = lib.repo_path()
repo = pygit2.Repository(repo_path)
index = lib.git_sort.SortIndex(repo)
try:
with open("series.conf") as f:
lines = f.readlines()
except IOError as err:
print("Error: could not open series file, %s" % (err,), file=sys.stderr)
sys.exit(1)
try:
before, inside, after = series_conf.split(lines)
current_entries = lib.parse_inside(index, inside, False)
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
if list(filter(lib.tag_needs_update, current_entries)):
print("Error: Some Git-repo tags for patches currently in series.conf "
"are outdated. Please run series_sort first and commit the "
"result before adding new patches.", file=sys.stderr)
sys.exit(1)
current_names = set([entry.name for entry in current_entries])
current_revs = {rev : entry.name
for entry in current_entries
if entry.dest_head != git_sort.oot
for rev in entry.revs}
new_lines = set()
new_entries = []
for name in args.patches:
if name in current_names:
print("Error: patch \"%s\" is already in series.conf." % (name,),
file=sys.stderr)
sys.exit(1)
entry = lib.InputEntry("\t%s\n" % (name,))
new_lines.add(entry.value)
try:
entry.from_patch(index, name, git_sort.oot, True)
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
if entry.dest_head != git_sort.oot:
for rev in entry.revs:
try:
match = current_revs[rev]
except KeyError:
continue
else:
print("Warning: commit %s in new patch \"%s\" is already "
"present in patch \"%s\" from series.conf." % (
rev[12:], entry.name, match,), file=sys.stderr)
new_entries.append(entry)
try:
sorted_entries = lib.series_sort(index, current_entries + new_entries)
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)
cur_sorted_entries = collections.OrderedDict()
for head, lines in list(sorted_entries.items()):
current_lines = [line for line in lines if line not in new_lines]
if current_lines:
cur_sorted_entries[head] = current_lines
cur_inside = lib.flatten([
lib.series_header(inside),
lib.series_format(cur_sorted_entries),
lib.series_footer(inside),
])
if inside != cur_inside:
print("Error: Current series.conf is not sorted. "
"Please run series_sort first and commit the result before "
"adding new patches.", file=sys.stderr)
sys.exit(1)
new_inside = lib.flatten([
lib.series_header(inside),
lib.series_format(sorted_entries),
lib.series_footer(inside),
])
output = lib.flatten([
before,
new_inside,
after,
])
with open("series.conf", mode="w") as f:
f.writelines(output)
try:
lib.update_tags(index, list(filter(lib.tag_needs_update, new_entries)))
except exc.KSError as err:
print("Error: %s" % (err,), file=sys.stderr)
sys.exit(1)