Giter Club home page Giter Club logo

gradle-gitversioner's Introduction

Git Versioner for gradle

Deprecated

DON'T USE THIS SCRIPT! It contains bugs and can't be updated (due to the fact hundreds of projects pull it directly from master). Please use

About

Version numbers are hard. It was easier with SVN where the revision number got increased for every commit. Revision 342 was clearly older than revision 401. This is not possible in git because branching is so common (and that's a good thing). 342 commits could mean multiple commits on different branches. Not even the latest common commit in history is clear. This projects aims to bring the SVN simplicity and more back to git for your gradle (android) project.

####Read the story behind on medium

Idea

Just count the commits of the main branch (master or develop in most cases) as the base revision. The commits on the feature branch are counted too, but are shown separately.

This technique is often used and far better than just a SHA1 of the latest commit. But I think it gives too much insights of the project. Once a client knows commit count == version number they start asking why the commit count is so high/low for the latest release.

That's why this versioner adds the project age (initial commit to latest commit) as seconds part to the revision. By default, one year equals 1000. This means that the revision count increases every 8.67 hours. When you started your project half a year ago and you have 325 commits the revision is something around 825.

When working on a feature branch this versioner adds a two char identifier of the branch name and the commit count since branching. When you are building and you have uncommited files it adds the count of the uncommited files and "-SNAPSHOT"

Reading the Version

Normal build number

1083

1083: number of commits + time component. this revision is in the master branch.

On a feature branch

1083-dm4

-dm4: 4 commits since branching from revision 1083. First two [a-z] chars of the base64 encoded branch name. Clients don't have to know about your information and typos in branch names. But you have to be able to distinguish between different builds of different branches.

Build with local changes

1083-dm4(6)-SNAPSHOT

(6)-SNAPSHOT: 6 uncommited but changed files. Hopefully nothing a client will ever see. But you know that your version is a work in progress version with some local changes

Get it

Configure the plugin in you top level build.gradle. This makes total sense because it's the revision of the top level project and not of a single module.

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    // ...
}

// Optional: configure the versioner
ext.gitVersioner = [
        defaultBranch           : "develop",  // default "master"
        stableBranches          : ["master", "someOtherBranch"], // default [], the feature branch postfix (-dm4(6)) will not be appended on stable branches, all commits are included into the version number calculation
        yearFactor              : 1200, 	  // default "1000", increasing every 8.57h
        snapshotEnabled         : false,      // default false, the "-SNAPSHOT" postfix
        localChangesCountEnabled: false,       // default false, the (<commitCount>) before -SNAPSHOT
        shortName: { gitVersion ->            // optional closure to build a short name
          // allows you to add your own short name logic
          // All properties from gitVersion are available
          // can be used for CI `System.getenv("BUILD_NUMBER")`

          // i.e. use short sha1
          return gitVersion.commit.subSequence(0, 7)
        }
]
// import the script which runs the version generation
apply from: 'https://raw.githubusercontent.com/passsy/gradle-GitVersioner/master/git-versioner.gradle'

// variable `gitVersionName` can be used everywhere to get the revision name
println("versionName: $gitVersionName") // output: "versionName: 1083-dm4(6)-SNAPSHOT"

Consider using this cache plugin for offline support.

All inforamtion is not only available as a single String. You can create your own pattern using the ext.gitVersion Object

// get granular information with variable `gitVersion` of type `GitVersion`
println("version: ${gitVersion.version}") // output "version: 1083"

// see all available attributes
class GitVersion {
    String name;
    int version;
    String branchName;
    String shortBranch;
    int branchVersion;
    int localChanges;
    String commit;
}

Android

Display the version in your android app

app build.gradle

android {
    defaultConfig {
        ...

        buildConfigField 'String', 'REVISION', "\"$gitVersionName\""
    }
}

in your Activity

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	Toast.makeText(this, BuildConfig.REVISION, Toast.LENGTH_SHORT).show();
    }

License

Copyright 2016 Pascal Welsch

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

gradle-gitversioner's People

Contributors

passsy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gradle-gitversioner's Issues

Version Code is never got updated

Hi, @passsy , I am applying the the git-versioner.gradle into my project, and I have problem to get versionCode updated, no matter how many commits has been updated, but they are still 277.

I checked my git version, it is git version 2.10.1, and the git path is located at: /usr/local/bin/git

Has anyone meet the same issue?

Branchname prefix "feature" is handled correctly, "bugfix" is not

The version name for a feature branch like this "feature/XYZ-566-something" is correctly converted into the version name "-XYZ-566-something". And with that version name the name of the resulting .jar file is correct.

But if I use a different prefix, "bugfix" for example, the prefix will not be removed from the version name leading to a completely broken .jar name of my resulting artifact.

I would like to have the same behaviour for any prefix, simply remove it.

To reproduce:

  1. Create a simple java library project
  2. Create a branch named "bugfix/Something"
  3. Build the jar file

SNAPSHOT postfix

When I generate the version with ext.gitVersioner.snapshotEnabled in false, the version name still with -SNAPSHOT postfiex
image

Ignores the configurations

As per your sample code, I have the following pasted at the top of my build.gradle

ext.gitVersioner = [
        defaultBranch           : "develop",  // default "master"
        stableBranches          : ["master", "someOtherBranch"], // default [], the feature branch postfix (-dm4(6)) will not be appended on stable branches, all commits are included into the version number calculation
        yearFactor              : 1200, 	  // default "1000", increasing every 8.57h
        snapshotEnabled         : false,      // default false, the "-SNAPSHOT" postfix
        localChangesCountEnabled: false,       // default false, the (<commitCount>) before -SNAPSHOT
        shortName: { gitVersion ->            // optional closure to build a short name
            // allows you to add your own short name logic
            // All properties from gitVersion are available
            // can be used for CI `System.getenv("BUILD_NUMBER")`

            // i.e. use short sha1
            return gitVersion.commit.subSequence(0, 7)
        }
]
// import the script which runs the version generation
apply from: 'https://raw.githubusercontent.com/passsy/gradle-GitVersioner/master/git-versioner.gradle'
println("versionName: $gitVersionName")

The version name that it prints out when running a gradle sync:
versionName: 433(3)-SNAPSHOT

You'll note that the changesCount and SNAPSHOT flag are enabled, even though they are set to false in the configuration. Please help!

Gradle task get stuck trying to acquire a lock

Hi @passy.

We are applying the git-versioner.gradle into our project, and we are facing a problem.

When we run some gradle tasks that makes a call to version.gradle, the build get stuck trying to acquire a lock.

We run the task with the --debug option, I'm sending attached the debug tail, and the exact point the build stops.

Are we doing something wrong? Any clue will be very helpful.

Regards,
Fernando Cesar.

buildLock.txt

Script not running when uncommited changes in git

I have use the script now for a month. But now, when i have uncommited changes in mit git repo, the script is not running successful. Is there a debug view or something else? Gradle's debug says nothing. Just script applyed and running.

Kind regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.