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.
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."""
30 ofv
= config
.get(old_key
)
32 # Great, there's an explicitly recorded format version
33 # number, which means that the branch is initialized and
34 # of that exact version.
37 # Old name for the version info: upgrade it.
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.
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.
50 # The branch doesn't seem to be initialized at all.
53 def set_format_version(v
):
54 out
.info('Upgraded branch %s to format version %d' % (branch
, v
))
55 config
.set(key
, '%d' % v
)
58 if not os
.path
.isdir(d
):
66 if repository
.refs
.exists(ref
):
67 repository
.refs
.delete(ref
)
70 if get_format_version() == 0:
71 mkdir(os
.path
.join(branch_dir
, 'trash'))
72 patch_dir
= os
.path
.join(branch_dir
, 'patches')
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())
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
)
89 refs_base
+ '/' + patch
,
90 repository
.get_commit(top
),
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
)
101 config
.set('branch.%s.description' % branch
, desc
)
103 rm(os
.path
.join(branch_dir
, 'current'))
104 rm_ref('refs/bases/%s' % branch
)
105 set_format_version(2)
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