Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Tuesday, September 3, 2019

Remove git orphan branches

Quite often especially when you create a pull request for some short time you have a local branch and a corresponding remote one that you track. But after your pull request is merged and remote branch is removed you still have your local branch.

If you run command

git remote prune origin

It will check if branches that you track are still there, if not  then your local branches are orphans now.

If you run now

git branch -vv

Then you will get a nice info about each branch


Here we can see that branch_a is gone, git literally says it. This is our orphan branch. No we can run the following command to remove it.

git branch -D branch_a

But what if we have 5 or 10 branches that became orphans? It would be nice to have some helper function that automates this process.

If your working environment is windows you can easily add a helper function into your powershell profile. Just go to powershell console and run

notepad $profile

And add the code bellow at the end of your powershell profile.

function git-clean() {
 git branch -vv | Where {$_.Contains("gone")} | ForEach -Process { git branch -D $_.Split()[2] }
}

Now every time you open a powershell console it will scan your profile and import all functions declared there. Now you can simply run git-clean and it will do all magic.

As summary just run


git remote prune origin
git-clean

P.S. if you have just added this function you need first to restart you powershell console to make this function available.

Sunday, October 12, 2014

Compare arrays in powershell using Pester

Recently I had to create deployment scripts using powershell. At some point our team realised that we need some way to test behavior of our scripts. After googling and comparing different libraries we decided to go with pester. It fits our needs and is actively supported, plus it's really easy to integrate into CI server.

Let's take a look at very simple examples.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Describe 'Pester demo' {
 Context 'Basic math' {   

  It 'Should add 2 numbers' {
   $sum = 2 + 7

   $sum | Should Be 9
  }

  It 'Should deduct 2 numbers' {
   $sum = 10 - 3

   $sum | Should Be 7
  }
 }
}

Nothing special, assertions work as pipeline functions. I personally like that library, it supports different kind of assertions, the only problem that I had is how to assert arrays equality.
Let's try to comapre two arrays.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Describe 'Compare arrays' {
 Context 'There are 2 arrays' {
  $array1 = 1, 2, 3
  $array2 = 1, 2, 3

  It 'Should be green' {
   $array1 | Should Be $array2
  }
 }
}

Output







Looks ok, let's break the test.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Describe 'Compare arrays' {
 Context 'There are 2 arrays' {
  $array1 = 1, 2, 4
  $array2 = 1, 2, 3

  It 'Should be green' {
   $array1 | Should Be $array2
  }
 }
}

Output



It's not really clear  what is really broken.
Let's see how we can improve it:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Describe 'Compare arrays' {
 Context 'There are 2 arrays' {
  $array1 = 1, 2, 4
  $array2 = 1, 2, 3

  $arrayStr1 = $array1 -join ', '
  $arrayStr2 = $array2 -join ', '

  It 'Should be green' {
   $arrayStr1 | Should Be $arrayStr2
  }
 }
}

Output













Now it makes more sense.

Thursday, July 3, 2014

Ternary operation powershell

I googled for ternary operation in powershell and found basically nothing, at least nothing good. It's seems like operations is not supported out of the box. There is a suggestion how it can be done using hash table.
Something like this:


$x = @{$true=10; $false=5}[$y % 2 -eq 0]

We just create a hash table with 2 boolean keys and then in accessor block evaluate condition.
In order to make it clear it might be simplified like that:


$key = $y % 2 -eq 0
$values = @{$true=10; $false=5}
$x = $values[$key]

Works fine but in my opinion not easy to read. It's always possible to invent a wheel. To add some custome function, I created that one:


function when {
 Param(
  [bool]$expression = $(throw "Boolean expression is required"),
  $then = $(throw "-then value is required"),
  $otherwise = $(throw "-otherwise value is required")
 )
 
 if ($expression) {
  return $then
 }
 
 return $otherwise
}

Here is an example of usage


$x = when ($y % 2 -eq 0) -then 10 -otherwise 5

I think it quite easy to read, but there is one thing that I really don't want to do, to add this function to every place where I need. In case you have many scripts and some script with the common stuff like Common.ps1, then maybe it's ok, since Common.ps1 is already linked everywhere. But it would be really nice to have something simple. And there is an option. Thing is that keyword return in powershell is not so required. Both function bellow return the same value:


function f1() {
 return 10
}

function f2() {
 10
}

Now let's try to think about obvious way how to deal with conditional logic. There is IF statement. So we can set a variable like that:

if ($y % 2 -eq 0) {
 $x = 10
} else {
 $x = 5
}

Let's improve it:


$x = if ($true) {
 return 10
} else {
 return 5
}

And the last improvement:


$x = if ($true) { 10 } else { 5 }

quite simple and no additional function is required. And it supports evaluation of some complex functions:


$z = if ($true) { someFunction $y $x } else { otherFunction $y $x }