Example #1
0
cdiff_file cdiff_file_open (char* filename) {
    cdiff_file result = malloc(sizeof(struct cdiff_file_s));

    /* Store current working directory in case the application using this
     * function changes the cwd before closing the file. */
    corto_asprintf(&result->name, "%s/%s", corto_cwd(), filename);

    result->elements = NULL;
    result->legacyElements = NULL;
    result->isChanged = false;
    result->writeBuffer = CORTO_BUFFER_INIT;
    result->writeTo = 0;
    result->indent = 0;
    result->newLine = true;
    result->cur = NULL;

    char *source = corto_fileLoad(result->name);
    if (source) {
        result->isNew = false;

        /* Check for legacy */
        if (strstr(source, "$CORTO_GENERATED")) {
            if (strstr(source, "$begin") || strstr(source, "$header")) {
                result->legacyElements = cdiff_parseLegacy(source);
                if (!result->legacyElements) {
                    goto error;
                }

                result->isChanged = true;
                result->isNew = true;
            }
        }

        /* Parse file (only when not parsing legacy file) */
        if (!result->legacyElements) {
            result->elements = cdiff_parse(source);
            if (!result->elements) {
                goto error;
            }
        }

        free(source);

    } else {
        corto_lasterr(); /* silence warning */
        result->isNew = true;
    }

    return result;
error:
    if (source) {
        free(source);
    }
    return NULL;
}
Example #2
0
corto_int16 cortotool_publish(int argc, char *argv[]) {
    corto_ll silent, mute, notag, dirs, majorarg, minorarg, patcharg;
    corto_uint32 major = 0, minor = 0, patch = 0;

    CORTO_UNUSED(argc);

    corto_argdata *data = corto_argparse(
      argv,
      (corto_argdata[]){
        {"$0", NULL, NULL}, /* Ignore 'publish' argument */
        {"--silent", &silent, NULL},
        {"--mute", &mute, NULL},
        {"--notag", &notag, NULL},
        {"$?*", &dirs, NULL},
        {"$+major", &majorarg, NULL},
        {"$|minor", &minorarg, NULL},
        {"$|patch", &patcharg, NULL},
        {NULL}
      }
    );

    if (!data) {
        corto_error("corto: %s", corto_lasterr());
        goto error;
    }

    if (dirs) {
        corto_string dir = corto_llGet(dirs, 0);
        if (corto_chdir(dir)) {
            corto_error("corto: %s", corto_lasterr());
            goto error;
        }
    }


    if (!corto_fileTest("./.corto")) {
        corto_error("corto: invalid project directory");
        goto error;
    }

    if (!notag) {
        corto_error(
            "corto: tagging of repository not yet supported "
            "(use --notag to just increase version)");
        goto error;
    }

    corto_string version = corto_fileLoad(".corto/version.txt");

    /* Patch version */
    if (version) {
        char *v = version;

        /* Parse major version */
        char *ptr = strchr(version, '.');
        if (ptr) {
            *ptr = '\0';
            major = atoi(v);
            v = ptr + 1;
        }

        /* Parse minor version */
        ptr = strchr(v, '.');
        if (ptr) {
            *ptr = '\0';
            minor = atoi(v);
            v = ptr + 1;
        }

        /* Parse patch version */
        patch = atoi(v);
    }

    if (majorarg) {
        major++;
    }
    if (minorarg) {
        minor++;
    }
    if (patcharg) {
        patch++;
    }

    FILE *f = fopen(".corto/version.txt", "w");
    if (!f) {
        corto_error("failed to open '.corto/version.txt' (check permissions)");
        goto error;
    }

    fprintf(f, "%u.%u.%u\n", major, minor, patch);
    fclose(f);

    corto_argclean(data);
    corto_dealloc(version);

    if (!silent) {
        corto_print("corto: version updated to %u.%u.%u", major, minor, patch);
    }

    return 0;
error:
    return -1;
}