Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. /*   Self include. */
  2. #include "globals.h"
  3.  
  4. /*   Standard includes. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. /*   Own includes. */
  10. #include "defs.h"
  11. #include "git.h"
  12.  
  13.  
  14. /*   Global variables. */
  15.  
  16. /*   Version description definitions and global variables. */
  17. /*   XXXXXXXXXXXXX is versioned like
  18.  * - MAJOR.MINOR.REVISION if it is in a git tag, or
  19.  * - MAJOR.MINOR.REVISION.COMMIT_HASH if it is in a git clean status (no
  20.  * uncommitted changes pending), or
  21.  * - MAJOR.MINOR.REVISION.COMMIT_HASH(+|-)dev if it is in a git dirty
  22.  * status with uncommitted changes present. */
  23. /*   MAJOR, MINOR and REVISION are taken from defs.h. */
  24.  
  25. /*   Holds the commit hash, if applicable. */
  26. char * COMMIT_HASH = 0;
  27. /*   Holds the commit hash, if applicable, including dev status, if
  28.  * applicable. */
  29. char * COMMIT_HASH_INCLUDING_DEV_STATUS = 0;
  30. /*   Holds all applicable version information. */
  31. char * VERSION = 0;
  32. /*   Holds all applicable version information, with the program name
  33.  * prepended. */
  34. char * VERSION_INCLUDING_NAME = 0;
  35.  
  36.  
  37. /*   Program name. */
  38. char * program_name = "XXXXXXXXXXXXX";
  39.  
  40.  
  41. void fill_version()
  42. {
  43.     char * _git_last_commit, * major, * minor, * revision, * version;
  44.     extern char * COMMIT_HASH, * COMMIT_HASH_INCLUDING_DEV_STATUS, * VERSION,
  45.             * VERSION_INCLUDING_NAME;
  46.     major = malloc(1 + 1);
  47.     minor = malloc(1 + 1);
  48.     revision = malloc(2 + 1);
  49.     sprintf(major, "%d", MAJOR);
  50.     sprintf(minor, "%d", MINOR);
  51.     sprintf(revision, "%d", REVISION);
  52.     if (git_at_a_tag())
  53.     {
  54.         version = malloc(
  55.                 strlen(major) + 1 + strlen(minor) + 1 + strlen(revision) + 1);
  56.         strcpy(version, major);
  57.         strcat(version, ".");
  58.         strcat(version, minor);
  59.         strcat(version, ".");
  60.         strcat(version, revision);
  61.     }
  62.     else
  63.     {
  64.         if (git_working_directory_clean())
  65.         {
  66.             _git_last_commit = git_last_commit();
  67.             version = malloc(
  68.                     strlen(major) + 1 + strlen(minor) + 1 + strlen(revision) +
  69.                     1 + strlen(_git_last_commit) + 1);
  70.             strcpy(version, major);
  71.             strcat(version, ".");
  72.             strcat(version, minor);
  73.             strcat(version, ".");
  74.             strcat(version, revision);
  75.             strcat(version, ".");
  76.             strcat(version, _git_last_commit);
  77.             COMMIT_HASH_INCLUDING_DEV_STATUS = malloc(
  78.                     strlen(_git_last_commit) + 1);
  79.             strcpy(COMMIT_HASH_INCLUDING_DEV_STATUS, _git_last_commit);
  80.         }
  81.         else
  82.         {
  83.             _git_last_commit = git_last_commit();
  84.             version = malloc(
  85.                     strlen(major) + 1 + strlen(minor) + 1 + strlen(revision) +
  86.                     1 + strlen(_git_last_commit) + 1 + 1);
  87.             strcpy(version, major);
  88.             strcat(version, ".");
  89.             strcat(version, minor);
  90.             strcat(version, ".");
  91.             strcat(version, revision);
  92.             strcat(version, ".");
  93.             strcat(version, _git_last_commit);
  94.             strcat(version, "+");
  95.             COMMIT_HASH_INCLUDING_DEV_STATUS = malloc(
  96.                     strlen(_git_last_commit) + 1 + 1);
  97.             strcpy(COMMIT_HASH_INCLUDING_DEV_STATUS, _git_last_commit);
  98.             strcat(COMMIT_HASH_INCLUDING_DEV_STATUS, "+");
  99.         }
  100.         COMMIT_HASH = _git_last_commit;
  101.     }
  102.     VERSION = version;
  103.     VERSION_INCLUDING_NAME = malloc(
  104.             strlen(program_name) + 1 + strlen(VERSION) + 1);
  105.     strcpy(VERSION_INCLUDING_NAME, program_name);
  106.     strcat(VERSION_INCLUDING_NAME, " ");
  107.     strcat(VERSION_INCLUDING_NAME, VERSION);
  108.     free(major);
  109.     free(minor);
  110.     free(revision);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement