4 * @brief driver-gen git API.
6 * Driver-gen can set the driver under the git control.
7 * This file contains all the utils to work with git.
9 * @author Copyright (C) 2010 CERN. Yury GEORGIEVSKIY <ygeorgie@cern.ch>
11 * @date Created on 01/02/2010
13 * @section license_sec License
14 * Released under the GPL
16 #define _GNU_SOURCE /* asprintf rocks */
17 #include <sys/types.h>
18 #include <sys/dir.h> /* for dirent, opendir */
19 #include "dg-git-lib.h"
22 * @brief Checks if driver is already controlled by git
24 * @param md -- module description
26 * @return 0 -- no yet supervised
27 * @return 1 -- already supervised
29 static uint8_t git_already_supervised(struct mdescr
*md
)
35 asprintf(&dname
, "%s%s", get_msd(NULL
), "/.git");
40 if (dname
) free(dname
);
41 if (dir
) closedir(dir
);
46 * @brief Initialize empty git repository
48 * @param md -- module description
51 static int git_init(void)
55 FILE *gifd
= OpenFile(EXACT_FT
, LOC_MODSRC
, ".gitignore");
57 asprintf(&str
, "%s%s%s", "git-commit -sm\"", TranslationGetModName(),
58 " driver framework initial release\"");
59 Translate(gifd
, "common", ".gitignore");
62 getcwd(cwd
, sizeof(cwd
)); /* save cwd */
67 system("git-tag -a dg2-v001 -m\"Automatically generated Version#1\"");
75 * @brief Check, if driver framework repo is in the consistent state.
77 * @param md -- module description
78 * @param fnp -- untracked filenames goes here
80 * Consisten state means that there are no modified files, that are not
81 * yet commited. Untracked files are allowed.
83 * @return number of untracted files.
84 * @return -1 -- non-constistent
86 static int git_check_consistency(struct mdescr
*md
, char ***fnp
)
89 char status
[4], fname
[512];
91 static char *fn
[512] = { NULL
};
94 getcwd(cwd
, sizeof(cwd
)); /* save cwd */
97 f
= popen("git-status -s 2>/dev/null", "r");
98 while (fscanf(f
, "%s %s\n", status
, fname
) != EOF
) {
99 if (strncmp(status
, "??", 2)) {
106 asprintf(&fn
[i
++], "%s", fname
);
109 chdir(cwd
); /* back where we start from */
122 * @return <ReturnValue>
124 int git_get_driver_version(void)
131 getcwd(cwd
, sizeof(cwd
)); /* save cwd */
132 chdir(get_msd(NULL
));
134 f
= popen("git-tag 2>/dev/null", "r");
135 while ( (rc
= fscanf(f
, "dg2-v%d\n", &v
)) != EOF
) {
136 if (!rc
) { /* skip matching failure */
137 rc
= fscanf(f
, "%*s");
141 chdir(cwd
); /* back where we start from */
154 void git_reset_user_files(void)
157 char status
[4], fname
[512];
161 getcwd(cwd
, sizeof(cwd
)); /* save cwd */
162 chdir(get_msd(NULL
));
164 f
= popen("git-status -s 2>/dev/null", "r");
165 while (fscanf(f
, "%s %s\n", status
, fname
) != EOF
) {
166 if (!strncmp(status
, "M", 1) && strstr(fname
, "UserDefined")) {
167 snprintf(str
, sizeof(str
), "git-checkout -- %s", fname
);
172 chdir(cwd
); /* back where we start from */
176 * @brief Checks git version. Should be > 1.5.3
178 * If git was not found, or if it was,
179 * but its version is < 1.5.3 -- will return an error.
181 * @return 0 -- git version is OK
182 * @return -1 -- git was not found, or version is < 1.5.3
184 uint8_t git_check_version(void)
187 uint32_t v
[4] = { 0 };
189 FILE *f
= popen("git --version 2>/dev/null", "r");
191 fscanf(f
, "%s %s %d.%d.%d.%d\n", str
[0], str
[1], &v
[0], &v
[1], &v
[2], &v
[3]);
193 printf("git not found. Sorry...\n");
195 } else if (v
[0]*100 + v
[1]*10 + v
[2] < 153) {
196 printf("git version is smaller than needed (%d < %d). Sorry...\n",
197 v
[0]*100 + v
[1]*10 + v
[2], 153);
200 printf("git version (%d) is OK...\n", v
[0]*100 + v
[1]*10 + v
[2]);
208 * @brief Update Driver/Simulator Version files.
210 * Using perl to bump the version +1.
213 static void git_upgrade_version_files(void)
217 /* simulator version */
218 name
= GenerateFilename(SIM_FT
, "include", "Version", ".h", NULL
);
219 asprintf(&cmd
, "perl -i -ne 'if (/(.*CURRENT_VERSION) (\\d+)/)"
220 " { $a=$2+1; print \"$1 $a\n\"; } else { print; }' %s", name
);
225 name
= GenerateFilename(DRIVER_FT
, "include", "Version", ".h", NULL
);
226 asprintf(&cmd
, "perl -i -ne 'if (/(.*CURRENT_VERSION) (\\d+)/)"
227 " { $a=$2+1; print \"$1 $a\n\"; } else { print; }' %s", name
);
233 * @brief Create new driver or commit new changes in existing repository.
235 * @param dn -- driver name
236 * @param md -- module description
237 * @param registers --
238 * @param numRegisters --
240 * @param numBlocks --
243 * If driver framework already exists -- commit new changes in existing
244 * repository. Commit sequence rules are:
245 * 1. Check if repo is in consistent state, i.e. no modified
246 * files. Untracked files are allowed.
247 * 2. Get current version from git-tag.
248 * * Versions *should* be in a simple predefined format:
249 * dg2-vXXX -- where XXX is the number, that will be bumped
251 * dg2-vXXX -- means that this driver was generated
252 * by the Driver Gen2, and that automatically generated Version
253 * of this driver is XXX).
255 * * If [dg2-vXXX] not found -- will be set to [dg2-v001]
257 * 3. Record all untracked files/dirs (so that they will not
259 * 4. Switch to master branch (save previous location).
260 * 5. Apply new templates.
261 * 6. git-add them into staging area.
262 * 7. unstage untracked files (if any).
263 * 8. Commit changes into master branch.
264 * 9. Switch back to original branch.
266 * @return <ReturnValue>
268 uint8_t do_git_vme_driver(char *dn
, struct mdescr
*md
,
269 RegisterDef_t
*registers
, int numRegisters
,
270 BlockDef_t
*blocks
, int numBlocks
,
273 char **fn
; /* saved untracked filenames */
274 char cwd
[1024], str
[1024];
275 uint8_t already
= git_already_supervised(md
);
278 if (already
) { /* already under git */
279 rc
= git_check_consistency(md
, &fn
);
281 printf("%s git directory is NOT in a consistent state!\n"
282 "Clean it up first, then retry once more.\n", get_msd(NULL
));
285 GenerateVmeDriver(registers
, numRegisters
, blocks
,
288 getcwd(cwd
, sizeof(cwd
)); /* save cwd */
289 chdir(get_msd(NULL
));
290 system("git-checkout master");
291 git_reset_user_files();
292 git_upgrade_version_files();
294 system("git-add -A");
295 for (i
= 0; i
< rc
; i
++) {
296 /* reset untracked files */
297 snprintf(str
, sizeof(str
), "git-reset %s", fn
[i
]);
301 i
= git_get_driver_version() + 1;
303 snprintf(str
, sizeof(str
),
304 "git-commit -sm\"New automatically generated Version#%d\"", i
);
307 snprintf(str
, sizeof(str
),
308 "git-tag -a dg2-v%03d -m\"New automatically generated Version#%d\"",
312 chdir(cwd
); /* go back where we start from */
314 } else { /* this is new driver framework */
315 printf("Creating %s source code directory.\n", dn
);
316 GenerateVmeDriver(registers
, numRegisters
, blocks
,
323 uint8_t do_git_pci_driver(char *dn
, struct mdescr
*md
,
324 RegisterDef_t
*registers
, int numRegisters
,
325 BlockDef_t
*blocks
, int numBlocks
,