Use isort diagnostic with --diff option
[stgit.git] / stgit / lib / stackupgrade.py
blobba2dd265148d0ed8db018e8b7304ac39716eca41
1 import os
3 from stgit import utils
4 from stgit.config import config
5 from stgit.exception import StackException
6 from stgit.out import out
8 # The current StGit metadata format version.
9 FORMAT_VERSION = 3
12 def format_version_key(branch):
13 return 'branch.%s.stgit.stackformatversion' % branch
16 def update_to_current_format_version(repository, branch):
17 """Update a potentially older StGit directory structure to the latest
18 version. Note: This function should depend as little as possible
19 on external functions that may change during a format version
20 bump, since it must remain able to process older formats."""
22 branch_dir = os.path.join(repository.directory, 'patches', branch)
23 key = format_version_key(branch)
24 old_key = 'branch.%s.stgitformatversion' % branch
26 def get_format_version():
27 """Return the integer format version number, or None if the
28 branch doesn't have any StGit metadata at all, of any version."""
29 fv = config.get(key)
30 ofv = config.get(old_key)
31 if fv:
32 # Great, there's an explicitly recorded format version
33 # number, which means that the branch is initialized and
34 # of that exact version.
35 return int(fv)
36 elif ofv:
37 # Old name for the version info: upgrade it.
38 config.set(key, ofv)
39 config.unset(old_key)
40 return int(ofv)
41 elif os.path.isdir(os.path.join(branch_dir, 'patches')):
42 # There's a .git/patches/<branch>/patches dirctory, which
43 # means this is an initialized version 1 branch.
44 return 1
45 elif os.path.isdir(branch_dir):
46 # There's a .git/patches/<branch> directory, which means
47 # this is an initialized version 0 branch.
48 return 0
49 else:
50 # The branch doesn't seem to be initialized at all.
51 return None
53 def set_format_version(v):
54 out.info('Upgraded branch %s to format version %d' % (branch, v))
55 config.set(key, '%d' % v)
57 def mkdir(d):
58 if not os.path.isdir(d):
59 os.makedirs(d)
61 def rm(f):
62 if os.path.exists(f):
63 os.remove(f)
65 def rm_ref(ref):
66 if repository.refs.exists(ref):
67 repository.refs.delete(ref)
69 # Update 0 -> 1.
70 if get_format_version() == 0:
71 mkdir(os.path.join(branch_dir, 'trash'))
72 patch_dir = os.path.join(branch_dir, 'patches')
73 mkdir(patch_dir)
74 refs_base = 'refs/patches/%s' % branch
75 with open(os.path.join(branch_dir, 'unapplied')) as f:
76 patches = f.readlines()
77 with open(os.path.join(branch_dir, 'applied')) as f:
78 patches.extend(f.readlines())
79 for patch in patches:
80 patch = patch.strip()
81 os.rename(os.path.join(branch_dir, patch), os.path.join(patch_dir, patch))
82 topfield = os.path.join(patch_dir, patch, 'top')
83 if os.path.isfile(topfield):
84 top = utils.read_string(topfield)
85 else:
86 top = None
87 if top:
88 repository.refs.set(
89 refs_base + '/' + patch,
90 repository.get_commit(top),
91 'StGit upgrade',
93 set_format_version(1)
95 # Update 1 -> 2.
96 if get_format_version() == 1:
97 desc_file = os.path.join(branch_dir, 'description')
98 if os.path.isfile(desc_file):
99 desc = utils.read_string(desc_file)
100 if desc:
101 config.set('branch.%s.description' % branch, desc)
102 rm(desc_file)
103 rm(os.path.join(branch_dir, 'current'))
104 rm_ref('refs/bases/%s' % branch)
105 set_format_version(2)
107 # Update 2 -> 3
108 if get_format_version() == 2:
109 protect_file = os.path.join(branch_dir, 'protected')
110 if os.path.isfile(protect_file):
111 config.set('branch.%s.stgit.protect' % branch, 'true')
112 os.remove(protect_file)
113 set_format_version(3)
115 # compatibility with the new infrastructure. The changes here do not
116 # affect the compatibility with the old infrastructure (format version 2)
117 if get_format_version() == 3:
118 hidden_file = os.path.join(branch_dir, 'hidden')
119 if not os.path.isfile(hidden_file):
120 utils.create_empty_file(hidden_file)
122 # Make sure we're at the latest version.
123 fv = get_format_version()
124 if fv not in [None, FORMAT_VERSION]:
125 raise StackException(
126 'Branch %s is at format version %d, expected %d'
127 % (branch, fv, FORMAT_VERSION)
129 return fv is not None # true if branch is initialized